Noor
Noor

Reputation: 20168

JavaScript xmlhttp read from feed

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>

Upvotes: 2

Views: 364

Answers (1)

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

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

Upvotes: 1

Related Questions