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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:14:12  来源:igfitidea点击:

Difference between dataType jsonp and JSON

javascriptjqueryajaxjquery-ui

提问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

参考http://jqueryui.com/demos/autocomplete/remote-jsonp.html

回答by thecodeparadox

dataType: jsonpfor cross-domain request, that means request to different domain and dataType: jsonfor 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 服务使用回调函数和数据进行响应。