从提要读取JavaScript xmlhttp
我正在尝试使用javascript并使用xmlhttp进行读取。我收到一个错误,因为它无法读取从提要读取JavaScript xmlhttp,javascript,xml,feed,Javascript,Xml,Feed,我正在尝试使用javascript并使用xmlhttp进行读取。我收到一个错误,因为它无法读取 <script type="text/javascript"> url="http://search.yahooapis.com/ WebSearchService /V1/webSearch?appid=YahooDemo &query=persimmon&results=2"; var xmlhttp = null; if (w
<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>
url=”http://search.yahooapis.com/ WebSearchService/V1/webSearch?appid=YahooDemo&query=persimon&results=2“;
var xmlhttp=null;
if(window.XMLHttpRequest)
{
xmlhttp=新的XMLHttpRequest();
if(typeof xmlhttp.overrideMimeType!=“未定义”)
{
overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject)
{
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
其他的
{
警报('可能您的浏览器不支持xmlhttprequests?');
}
open('GET',url,true);
xmlhttp.send(空);
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
警惕(“成功”);
}
其他的
{
警报(“故障”);
}
};
除非您的网站位于search.yahooapi.com
上,否则您可能会遇到问题
这导致您的传出请求返回404
状态代码:
您应该使用而不是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>
JavaScript文件下载
函数yahooApi(resp){
var scriptEl=document.getElementById(“yahooApiJsonP”);
scriptEl.parentNode.removeChild(scriptEl);
控制台日志(resp);
}
window.onload=函数(){
var scriptEl=document.createElement(“脚本”);
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);
};
这是一个测试
这将发送请求,返回200 OK
状态:
它看起来也像:
出现“故障”错误,else零件警报中的块(“故障”)