dataType jsonp和JSON的区别。

时间:2021-06-21 16:34:37

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 autoload,查找远程-jsonp.html。这是ajax功能,但我打开控制台。我在我的控制台看不到任何请求……

What is difference between dataType;"jsonp" and dataType;"JSON"

数据类型之间的区别是什么?和数据类型;“jsonp 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
                        }
                    }));
                }
            });
        },

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

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

2 个解决方案

#1


27  

dataType: jsonp for cross-domain request, that means request to different domain and dataType: json for same domain-same origin request.

dataType: jsonp用于跨域请求,这意味着对不同域和数据类型的请求: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块。在URL的末尾添加一个“?callback=?”来指定回调。通过向URL附加查询字符串参数“_=[时间戳]”来禁用缓存,除非缓存选项设置为true。

Read about same origin policy

阅读相同原产地政策

Read more about jQuery AJAX

阅读更多关于jQuery AJAX的内容。

#2


7  

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服务使用回调函数和数据进行响应。

#1


27  

dataType: jsonp for cross-domain request, that means request to different domain and dataType: json for same domain-same origin request.

dataType: jsonp用于跨域请求,这意味着对不同域和数据类型的请求: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块。在URL的末尾添加一个“?callback=?”来指定回调。通过向URL附加查询字符串参数“_=[时间戳]”来禁用缓存,除非缓存选项设置为true。

Read about same origin policy

阅读相同原产地政策

Read more about jQuery AJAX

阅读更多关于jQuery AJAX的内容。

#2


7  

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服务使用回调函数和数据进行响应。