Javascript dataType jsonp 和 JSON 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10852652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between dataType jsonp and JSON
提问by Alizain Prasla
I download Jquery UI autoload, looking to remote-jsonp.html. This is ajax function but i open console.. I can't see any request in my console...
我下载了 Jquery UI 自动加载,寻找 remote-jsonp.html。这是ajax功能,但我打开控制台..我在我的控制台中看不到任何请求......
What is difference between dataType;"jsonp" and dataType;"JSON"
dataType;"jsonp" 和 dataType;"JSON" 有什么区别
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function( data ) {
response( $.map( data.geonames, function( item ) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
Referencehttp://jqueryui.com/demos/autocomplete/remote-jsonp.html
回答by thecodeparadox
dataType: jsonp
for cross-domain request, that means request to different domain and dataType: json
for same domain-same origin request.
dataType: jsonp
对于跨域请求,这意味着请求到不同的域和dataType: json
相同域的同一源请求。
Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "_=[TIMESTAMP]", to the URL unless the cache option is set to true.
使用 JSONP 加载到 JSON 块中。添加一个额外的“?callback=?” 到 URL 的末尾以指定回调。通过将查询字符串参数“_=[TIMESTAMP]”附加到 URL 来禁用缓存,除非缓存选项设置为 true。
Read about same origin policy
阅读同源政策
Read more about jQuery AJAX
阅读有关jQuery AJAX 的更多信息
回答by Jeff LaFay
With JSONP you shouldn't see an ajax request if that's what you're looking for. You should however see a request for the resource because JSONP is used for cross domain calls to pull in data from different domains.
使用 JSONP,如果这是您要查找的内容,则不应看到 ajax 请求。但是,您应该会看到对资源的请求,因为 JSONP 用于跨域调用以从不同域中提取数据。
It returns your JSON data wrapped in a function name. jQuery handles the function name behind the scenes and passes the data into your success handler. The data is loaded by dynamically creating a script element with the src attribute pointing to the service being called and then attached to the browser's DOM. Then the browser makes a request to the resource and the web service responds with the callback function and data.
它返回包含在函数名称中的 JSON 数据。jQuery 在幕后处理函数名称并将数据传递到您的成功处理程序。通过动态创建一个脚本元素来加载数据,其中 src 属性指向被调用的服务,然后附加到浏览器的 DOM。然后浏览器向资源发出请求,Web 服务使用回调函数和数据进行响应。