gpt4 book ai didi

从提要中读取的 JavaScript xmlhttp

转载 作者:行者123 更新时间:2023-11-30 06:02:01 25 4
gpt4 key购买 nike

我正在尝试使用 javascript 并阅读 from http://search.yahooapis.com/ WebSearchService /V1/webSearch?appid=YahooDemo &query=persimmon&results=2使用 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>

最佳答案

除非您的网站托管在 search.yahooapis.com 上,否则您可能会遇到 Same Origin Policy .

这会导致您的传出请求返回 404 状态码:

enter image description here

你应该使用 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


它看起来也像this service has been shut down :

enter image description here

关于从提要中读取的 JavaScript xmlhttp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7826083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com