使用 jQuery 从子域获取 JSON
我有 user1.mydomain.com
和 user2.mydomain.com
域。我使用 api.mydomain.com 通过 AJAX/JSON 处理我的 Web 应用程序。因此,我想使用 jQUery 从 user1.mydomain.com
向 api.mydomain.com/projects
发出 POST 请求,如下所示:{'action' :'getActiveProjects'}
以 JSON 格式获取 user1 的活动项目列表。我找到了 $.getJSON 方法,但似乎没有选项可以将一些数据发送到服务器,只有 GET 方法。我面临的另一个问题是同源政策。那么,如何将一些 JSON 发布到另一个子域上的服务器并获得 JSON 响应结果呢?
I have user1.mydomain.com
and user2.mydomain.com
domains. I use api.mydomain.com
to deal with my web app over AJAX/JSON. So, I want to make a POST request from user1.mydomain.com
to api.mydomain.com/projects
using jQUery something like this: {'action':'getActiveProjects'}
to get list of active projects for user1 in JSON as a result. I found $.getJSON
method but it seems there is no option for sending some data to server, just GET method. The other problem I face is same origin policy. So, how can I POST some JSON to server on another subdomain and get JSON response as a result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过指定
使用
。链接文档中的详细信息。您的服务器必须使用 JSON-P 进行响应,而不仅仅是 JSON,但这非常简单如果你控制了服务器就要做。$.ajax
和 JSON-P >数据类型:“jsonp”或者,如果您只需要支持相当新的浏览器(而不是 IE),您可以将服务器设置为支持 CORS。但这仅在最近的浏览器中支持,尽管 IE8 支持它,但它并不通过通常的
XMLHttpRequest
对象透明地支持它,而是需要一个完全不同的传输对象(XDomainRequest),jQuery 还不能自动为您处理。
下面是一个使用 jQuery 的 JSON-P 示例:
Live copy
在服务器上,您会看到 jQuery 添加了 < URL 的 code>callback 参数,例如在上面它是
http://jsbin.com/ubucu4?callback=exampleCallback
但如果你喜欢 jQuery 控制它的名称将是更有异国情调一点。您的服务器端代码应该构建一个 JavaScript 函数调用的响应,调用该函数。我在上面的示例中的回应是:这一切都发生是因为而不是使用
XMLHttpRequest
,它受 同源策略,JSON-P使用动态添加的script
标签(这很好)。在我的示例中,标签将类似于浏览器将检索脚本(即您的 JSON-P 响应)并执行它。这意味着回调被调用,并且您的数据被提供给您的脚本。
从技术上讲,您的 JSON-P 响应并不是 JSON;而是 JSON。它是 JavaScript,因此,您必须仅将 JSON-P 与您信任的服务器(例如您自己的子域服务器)一起使用,因为您是直接将代码注入到页面中。否则,如果您使用一些不信任的服务器,则注入的代码很可能会读取页面上的信息并将其发送给第三方。让你的口号保持谨慎。
Using
$.ajax
and JSON-P by specifying thedataType: "jsonp"
. Details in the linked docs. Your server will have to respond with JSON-P rather than just JSON, but that's pretty easy to do if you control the server.Alternately, if you only need to support fairly recent browsers (and not IE), you can set up your server to support CORS. But that's only supported in recent browsers, and although IE8 supports it, it doesn't support it transparently through the usual
XMLHttpRequest
object, but instead requires a completely different transport object (XDomainRequest
), which jQuery doesn't handle automatically for you (yet).Here's a JSON-P example using jQuery:
Live copy
On the server, you'll see that jQuery has added a
callback
parameter to the URL, e.g. in the above it would behttp://jsbin.com/ubucu4?callback=exampleCallback
but if you like jQuery control it the name will be a bit more exotic. Your server-side code should construct a response that is a JavaScript function call, calling that function. My response in the above example is:This all happens because instead of using
XMLHttpRequest
, which is subject to the Same Origin Policy, JSON-P uses a dynamically-addedscript
tag (which is fine). In my example, the tag will look something likeThe browser will retrieve the script, which is your JSON-P response, and execute it. That means the callback gets called, and your data is supplied to your script.
Your JSON-P response isn't, technically, JSON; it's JavaScript, and for that reasons it's essential that you only use JSON-P with servers you trust (such as your own subdomain servers), since you're injecting code directly into the page. Otherwise, if you're using some server you can't trust, the code that gets injected may well read information off the page and send that to some third party. Let caution by your watchword.
您不能使用 Ajax/JSON,因为子域是单独的域。您可以,但是请使用JSONP。 jQuery 内置了此功能,因此您只需在请求中指定即可。查看相关的文档。您不能将 POST 与 JSONP 结合使用(这受到该技术工作方式的限制),但没有其他方法可以执行跨浏览器跨域请求。
You cannot use Ajax/JSON, since subdomains are individual domains. You can, however use JSONP. jQuery has this built in, so you only need to specify that in your request. Have a look at the relevant docs. You cannot use POST with JSONP (this is limited by the way this technique works), but there is no other way to do cross-browser cross-domain requests.
将
document.domain
设置为主域此处了解更多信息
Set the
document.domain
to the main domainMore info here