跨站点AJAX请求 [英] Cross-site AJAX requests

查看:235
本文介绍了跨站点AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个网站一个Ajax请求托管在另一个域中的REST Web服务。

I need to make an AJAX request from a website to a REST web service hosted in another domain.

尽管这是在Internet Explorer中工作得很好,其他浏览器如Mozilla和谷歌浏览处以更加严格的安全限制,其中规定禁止跨站点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 code,使异步调用:

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 可以提供帮助。

NB youll必须改变你的邮件使用JSON而不是XML

NB youll have to change your messages to use json instead of xml

修改

大型网站,如Flickr和微博的支持与JSONP回调等

Major sites such as flickr and twitter support jsonp with callbacks etc

这篇关于跨站点AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆