2011-10-19 7 views
2

私はjavascriptを使い、xmlhttpを使ってfrom http://search.yahooapis.com/ WebSearchService /V1/webSearch?appid=YahooDemo &query=persimmon&results=2を読み込もうとしています。あなたのウェブサイトは、あなたはおそらくSame Origin Policyに遭遇しているsearch.yahooapis.com上でホストされていない限り、それはJavaScript xmlhttpからのフィード

<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> 
+0

正確なエラー何? –

+0

エラーは「失敗」です。else部分のアラートのブロック(「失敗」) – Noor

答えて

1

を読み取ることができないので、私はエラーを取得しています。 、

<!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> 

これはリクエストが送信されます:あなたはXMLHttpRequestの代わりにJSONPを使用する必要があります

enter image description here

:これは404ステータスコードを返すように発信要求を引き起こしている

200 OKステータスを返します:enter image description here


またthis service has been shut down次のようになります。

enter image description here

関連する問題