Если ваш веб-сайт не размещен на search.yahooapis.com
, вы, вероятно, сталкиваетесь с той же политикой происхождения .
В результате ваш исходящий запрос возвращается с кодом состояния 404
:
![enter image description here](https://i.stack.imgur.com/Mz2Mj.png)
Вы должны использовать JSONP вместо 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>
Это отправит запрос, который вернет 200 OK
статус: ![enter image description here](https://i.stack.imgur.com/PU75q.png)
Это также выглядит как эта служба была закрыта :
![enter image description here](https://i.stack.imgur.com/n3IR0.png)