跨站 AJAX 请求 [英] Cross-site AJAX requests
问题描述
我需要从网站向托管在另一个域中的 REST 网络服务发出 AJAX 请求.
I need to make an AJAX request from a website to a REST web service hosted in another domain.
尽管这在 Internet Explorer 中运行良好,但其他浏览器(例如 Mozilla 和 Google Chrome)施加了更严格的安全限制,禁止跨站点 AJAX 请求.
Although this is works just fine in Internet Explorer, other browsers such as Mozilla and Google Chrome impose far stricter security restrictions, which prohibit cross-site AJAX requests.
问题是我无法控制站点所在的域和 Web 服务器.这意味着我的 REST Web 服务必须在其他地方运行,而且我无法设置任何重定向机制.
The problem is that I have no control over the domain nor the web server where the site is hosted. This means that my REST web service must run somewhere else, and I can't put in place any redirection mechanism.
这是进行异步调用的 JavaScript 代码:
Here is the JavaScript code that makes the asynchronous call:
var serviceUrl = "http://myservicedomain";
var payload = "<myRequest><content>Some content</content></myRequest>";
var request = new XMLHttpRequest();
request.open("POST", serviceUrl, true); // <-- This fails in Mozilla Firefox amongst other browsers
request.setRequestHeader("Content-type", "text/xml");
request.send(payload);
如何在 Internet Explorer 之外的其他浏览器中使用此功能?
How can I have this work in other browsers beside Internet Explorer?
推荐答案
也许 JSONP 可以提供帮助.
maybe JSONP can help.
注意,您必须将消息更改为使用 json 而不是 xml
NB youll have to change your messages to use json instead of xml
编辑
主要网站如 flickr 和 twitter 支持 jsonp 和回调等
Major sites such as flickr and twitter support jsonp with callbacks etc
这篇关于跨站 AJAX 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!