1
我想用javascript的Bing搜索API。 实际上,我希望用户写一些东西并查询Bing以获取图像。bing search api ajax不工作
所以,我试着用ajax。 如果我直接尝试url http://api.search.live.net/xml.aspx?Appid=[YOURAPIKEY]&sources=image&query=home(使用浏览器),我得到一个xml文档。
但如果我使用XMLHttpRequest它不起作用。
<html>
<body>
<script>
var xhr = new XMLHttpRequest();
var url="http://api.search.live.net/xml.aspx?Appid=[YOURAPIKEY]&sources=image&query=home"
xhr.open("GET", url, true);
xhr.onreadystatechange=function(){
/*if(xhr.readyState == 4 && xhr.status == 200) {
document.write(xhr.responseText);
}*/
alert("state: "+xhr.readyState +" status: "+xhr.status +" statusText: "+xhr.statusText +" respText: "+xhr.responseText);
};
xhr.send(null);
</script>
</body>
</html>
问题: 1)为什么从上面的代码不能正常工作? 2)任何其他方式来做到这一点没有XMLHttpRequest?
谢谢。
btw。我只是想解决这个Firefox和没有外部库(jQuery等)。
你不能做XHR跨域。你需要JSONP <---我想,这将发生在Bing或谷歌或雅虎apis,对吧? – jhon 2010-05-30 06:17:37
是的,您将遇到与任何XHR到不同域的问题。我知道Google有特定的服务可以让您在没有XHR的情况下实现此目标;也许Bing有类似的东西。 – dmazzoni 2010-05-30 06:20:35