请在jQuery中解释JSONP [英] Please explain JSONP in jQuery

查看:80
本文介绍了请在jQuery中解释JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解jQuery.ajax的文档,特别是两个选项:jsonpjsonpCallback,所以有人可以这么乐于解释吗?

I can't understand the doc of the jQuery.ajax, specifically two options: jsonp and jsonpCallback, so can somebody be so pleasant to explain?

我的理解是jsonp是服务器期望的GET参数的名称(通常是回调"),而jsonpCallback是用于包装响应的函数的名称.看起来很简单.

What I do understand is that jsonp is a name of a GET parameter which server expects (usually 'callback') and jsonpCallback is a name of a function to wrap a response. Seems simple.

但是 jQuery.ajax文档上的解释使这一点变得有些复杂.我想在这里引用jsonp选项的全文,并用粗体标记我不知道的地方:

But the explanation at the jQuery.ajax doc makes this a bit complicated. I would like to cite the complete text for jsonp option here and mark with bold what is obscure to me:

jsonp

jsonp

在jsonp请求中覆盖回调函数名称.这个值 将会在'callback =?'部分中代替"callback"使用 网址中的查询字符串.因此{jsonp:'onJSONPLoad'}将导致 'onJSONPLoad =?'传递到服务器.从jQuery 1.5开始,设置 jsonp选项设置为false可防止jQuery添加?callback" URL字符串或尝试使用"=?" 进行转换.在 在这种情况下,您还应该显式设置jsonpCallback 设置. 例如,{jsonp:false,jsonpCallback:"callbackName"}

Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

所以问题是:

1."=?" 'callback =?'(额外的问号)是什么意思?当我像这样执行JSONP AJAX请求时:

1.What does it mean "=?" or 'callback=?' (extra question mark)? When I perform a JSONP AJAX-request like here:

 $.ajax('http://fake.com',{
     dataType: 'jsonp',
     success: function(data) {console.log(data);}
 }); 

URL看起来像这样,没有这样的问号:

The URL looks like this, there is no such question mark:

http://fake.com/?callback=jQuery18104830878316494931_1352981996209&_=1352981999411

2.额外的参数(下划线)是什么?_=1352981999411?

2.What is the extra parameter (underscore) _=1352981999411?

3.In this case, you should also explicitly set the jsonpCallback setting是什么意思?我看不到任何相互关系.如果按照文档中的说明设置{ jsonp: false, jsonpCallback: "callbackName" },查询将如下所示:

3.What do the words In this case, you should also explicitly set the jsonpCallback setting mean? I can't see any interrelationship. If I set { jsonp: false, jsonpCallback: "callbackName" }, as it is said in the doc the query will look like this:

http://fake.com/?_=1352981999411

根本没有使用"callbackName",那么为什么要指定它呢?

There is no use of the "callbackName" at all so why to specify it?

我的赞赏.

推荐答案

回调名称是可选的,如果不提供,则使用默认名称.因此,如果省略onJsonLoad,您将得到callback.

The callback name is optional, if you don't supply one it uses a default one. Hence, if you leave out onJsonLoad you just get callback.

发生的事情是JSON被一个函数包装,因为JSONP通过<script>标签起作用,所以您的常规JSON:

What happens is the JSON is wrapped with a function, because JSONP works through the <script> tag, so your normal JSON:

{prop: value}

成为:

callback({prop: value});

在加载脚本标签后,jQuery可以调用此函数并获取JSON数据.

When the script tag is loaded jQuery can then call this function and get back the JSON data.

_=123456788只是一个Date.getTime(),用于阻止缓存请求.

The _=123456788 is just a Date.getTime() to stop the request from caching.

这篇关于请在jQuery中解释JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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