XMLHttpRequest 和 Bing 翻译器
如果以下代码我没有得到 status 200
和 responseText
。但此 URL 有效:http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?appId=F1B50AB0743B541AA8C07089042D7B57E9B28D25
。同样在 Wireshark 中,我看到我得到了预期的 status 200
和 data
。我的 Javascript 代码有什么问题?
function btnclick()
{
var http = new XMLHttpRequest();
var str = "";
http.open('GET', 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?appId=F1B50AB0743B541AA8C07089042D7B57E9B28D25',
true);
http.onreadystatechange = function (evt)
{
if (http.readyState == 4 && http.status == 200)
{
alert(http.responseText);
}
}
http.send(null);
}
If the following code I don't get status 200
and responseText
. But this URL works: http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?appId=F1B50AB0743B541AA8C07089042D7B57E9B28D25
. Also in Wireshark I see that I get status 200
and data
which is expected. What is the problem with my Javascript code?
function btnclick()
{
var http = new XMLHttpRequest();
var str = "";
http.open('GET', 'http://api.microsofttranslator.com/V2/Http.svc/GetLanguagesForTranslate?appId=F1B50AB0743B541AA8C07089042D7B57E9B28D25',
true);
http.onreadystatechange = function (evt)
{
if (http.readyState == 4 && http.status == 200)
{
alert(http.responseText);
}
}
http.send(null);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试制作跨域 XHR。这违反了同源政策。
您可以构建一个服务器端代理,并使用 XHR 请求该代理。
You are trying to make a cross domain XHR. That violates same origin policy.
You could build a server side proxy, and request that with XHR.
如果你正在做AJAX,你可能想使用AJAX版本来克服跨域问题。
http://msdn.microsoft.com/en-us/library/ff512404.aspx
If you are doing AJAX, you might want to use the AJAX version to overcome the cross domain issue.
http://msdn.microsoft.com/en-us/library/ff512404.aspx