Ajax、jQuery 和 JSONP
似乎我可以使用 jQuery 的 getJSON 方法从不同的域获取 JSON 数据(请参阅:http://docs.jquery. com/Getjson)。但是,这仅适用于 HTTP GET。
如果我需要 POST 某些内容并获取 JSON 响应怎么办?我该如何在 jQuery/Ajax 中做到这一点?
It seems like I can get JSON data from a different domain using jQuery's getJSON method (see: http://docs.jquery.com/Getjson). However, this works only for HTTP GET.
What if I needed to POST something and get the JSON response? How would I do that in jQuery/Ajax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自版本 1.6.1 起,无法在所有浏览器中仅使用 jQuery 从客户端向远程服务器发送 POST 请求。如果您尝试向与文档不同的域中的服务器发出任何类型的 XHttpRequest,某些浏览器将无法完成它。对远程服务器的 JSONP 请求是通过创建脚本标记来处理的,该脚本标记的 src 是添加了查询参数的 API url,包括回调方法名称。因为脚本可以从任何域加载,所以这是可行的,但它限制了您只能进行 GET 请求。远程主机返回脚本的主体,即对生成的 javascript 对象调用的回调。 jQuery 通常会为您创建回调函数,并从中调用您在 getJSON 方法参数中提供的匿名回调函数。
有一些新兴标准,CORS 和 UMP(另请参阅比较),一些浏览器支持,但不是以标准化方式(IE 的做法有所不同)。有插件为那些支持 CORS 的浏览器提供部分支持。不知道它们的工作效果如何,除非浏览器支持,否则它们将无法工作。
It is not possible to POST requests to a remote server from the client using jQuery alone as of version 1.6.1 in all browsers. If you attempt to make an XHttpRequest of any sort to a server in a different domain than the document, some browsers will simply fail to complete it. The JSONP requests to remote servers are handled by creating a script tag, the src for which is the API url with the query parameters added, including a callback method name. Because scripts can be loaded from any domain, this works, but it limits you to GET requests. The remote host returns the body of the script which is the callback invoked on the resulting javascript object. jQuery typically creates the callback function for you and from it calls the anonymous callback function you supply in the
getJSON
method parameters.There are emerging standards, CORS and UMP (see also the comparison), that some browsers support but not in standardized ways (read IE does it differently). There are plugins to provide partial support for those browsers that do support CORS. No idea how well they work and they won't work unless the browser supports it.
其他答案并不完全正确。如果您可以控制服务器,这是可能的。
看:
W3C - 跨源资源共享
http://www.w3.org/TR/cors/
本质上,客户端发送一个“飞行前”选项 HTTP 请求,如果从服务器收到正确的响应,则继续执行常规操作。 (网上有很多例子......除非你需要我,我不会详细介绍)。
我知道这可能不适用于所有情况(例如,我不确定 IE5/5.5 是否支持此功能...但我相信 IE6 可以)...但是如果您正在开发 HTML5 应用程序,并且您可以控制服务器,这对您来说可能是一种可能性。
注意:顺便说一句 - 当然,考虑到我更喜欢 JSONP 的选项。更少出错。
编辑:这里似乎有很多混乱,所以让我举一个例子来说明如何使用 .NET / WCF 来做到这一点(我认为其中一些来自某处的一篇文章,而它的其他部分是内部开发的...所以如果其中一些来自其他地方,我提前道歉,因为没有给予应有的信任):
并且在 Web.config 中:
The other answers aren't entirely true. This is possible if you have control over the server.
See:
W3C - Cross-Origin Resource Sharing
http://www.w3.org/TR/cors/
Essentially, the client sends a "pre-flight" OPTIONS HTTP request, and, if the correct response is received from the server, it continues with it's regular operations. (There are plenty of examples online... Unless you need me to, I won't get into the details).
I understand this may not work in all scenarios (for example, I'm not sure if IE5/5.5 supports this or not... but I believe IE6 does)... but if you're working on an HTML5 app, and you have control over the server, this could be a possibility for you.
NOTE: Just an aside - Given the option I'd prefer JSONP, of course. Less to go wrong.
EDIT: There seems to be a lot of confusion here, so let me give an example of how one might do this using .NET / WCF (I think some of this came from an article somewhere, and other parts of it were developed in house... so if some of it came from somewhere else, I apologize in advance for not giving the due credit):
And in the Web.config:
简而言之:JsonP是一种仅限于GET请求的跨域技术。
in short: JsonP is a cross-domain technique limited to GET request.