views:

28

answers:

1

I think the code is right, but I don't get any response in my second alert. When I go to the website given in the first alert, I get the text I want. Why doesn't it work?

<script type="text/javascript">

function getimages(q) {
   bossimagesrequest = new XMLHttpRequest;
   var requri = "http://boss.yahooapis.com/ysearch/images/v1/"+escape(q)+"?appid=yahoobosskey&amp;filter=true&amp;count=10";
   bossimagesrequest.open("GET", requri, true);
   bossimagesrequest.onreadystatechange = function() {
      if (bossimagesrequest.readyState === 4) {
         alert(requri);
         alert(bossimagesrequest.responseText);
      }
   };
   bossimagesrequest.send();
}
</script>

I did replace "yahoobosskey" with my API key, so that's not the problem.

A: 

It looks like you have bumped into the same origin policy. You may have to use a relative path instead of your absolute http://boss.yahooapis.com/... path.

Related: Empty responseText from XMLHttpRequest

You may also be interested in checking out the following Stack Overflow community wiki post:

Daniel Vassallo
So I can't use Yahoo BOSS with AJAX? I think it would be silly when Yahoo blocked other servers from BOSS, because then you have to do very much to circumvent the block. Don't I have to use AJAX for Yahoo BOSS?
@user376919: The Same Origin Policy blocks anything that does not originate from the same domain of the web document. Even a different port number is not allowed. I have no experience with Yahoo's BOSS, but you may want to check if a [JSONP](http://en.wikipedia.org/wiki/JSON#JSONP) service is served. Otherwise, you may want to check out the methods described in the SO post I linked to.
Daniel Vassallo
... For example, I noticed that [Yahoo's Search API](http://developer.yahoo.com/common/json.html#callbackparam) serves JSONP. It's described as JSON using Callback.
Daniel Vassallo