2

I'm trying to use javascript and read from http://search.yahooapis.com/ WebSearchService /V1/webSearch?appid=YahooDemo &query=persimmon&results=2 using xmlhttp. I'm getting an error because it cannot read

<script type="text/javascript">
       url="http://search.yahooapis.com/ WebSearchService /V1/webSearch?appid=YahooDemo &query=persimmon&results=2";
       var xmlhttp = null;
       if (window.XMLHttpRequest) 
       {
          xmlhttp = new XMLHttpRequest();
          if ( typeof xmlhttp.overrideMimeType != 'undefined') 
          {
             xmlhttp.overrideMimeType('text/xml');
          }
       } 
       else if (window.ActiveXObject) 
       {
          xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
       } 
       else 
       {
          alert('Perhaps your browser does not support xmlhttprequests?');
       }

       xmlhttp.open('GET', url, true);
       xmlhttp.send(null);
       xmlhttp.onreadystatechange = function() 
       {
           if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
           {
            alert("success");
           }
           else 
           {
            alert("failure");
           }
      };
</script>
1
  • There error is 'failure', the block in the else part alert("failure") Commented Oct 19, 2011 at 18:34

1 Answer 1

1

Unless your web site is hosted on search.yahooapis.com you're probably encountering the Same Origin Policy.

This is causing your outgoing request to return with a 404 status code:

enter image description here

You should be using JSONP instead of XMLHttpRequest:

<!DOCTYPE html>
<html>
 <head>
     <title>JavaScript file download</title>
<script type="text/javascript">
     function yahooApi(resp) {
         var scriptEl = document.getElementById("yahooApiJsonP");
         scriptEl.parentNode.removeChild(scriptEl);
         console.log(resp);
     }

     window.onload = function() {
         var scriptEl = document.createElement("script");
         scriptEl.id = "yahooApiJsonP";
         scriptEl.src = "http://search.yahooapis.com/WebSearchService/V1/webSearch?output=json&callback=yahooApi&appid=YahooDemo&query=persimmon&results=2";
         document.body.appendChild(scriptEl);
     };
</script>
 </head>
 <body>
    <p>This is a test</p>
 </body>
</html>

This will send the request, which will return a 200 OK status:enter image description here


It also looks like this service has been shut down:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.