dataType jsonp和JSON之间的区别

时间:2012-06-01 14:57:33

标签: javascript jquery ajax jquery-ui

我下载了Jquery UI自动加载,寻找remote-jsonp.html。这是ajax功能,但我打开控制台..我在控制台中看不到任何请求......

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
                        }
                    }));
                }
            });
        },

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

2 个答案:

答案 0 :(得分:28)

dataType: jsonp用于跨域请求,即对不同域的请求和dataType: json对同一域相同来源请求的请求。

  

使用JSONP加载JSON块。添加额外的“?callback =?”到了   您的网址末尾指定回调。通过附加禁用缓存   除非缓存,否则查询字符串参数“_ = [TIMESTAMP]”到URL   选项设置为true。

了解 same origin policy

详细了解 jQuery AJAX

答案 1 :(得分:8)

使用JSONP,如果您正在寻找,那么您不应该看到ajax请求。但是,您应该看到对资源的​​请求,因为JSONP用于跨域调用以从不同域中提取数据。

它返回包含在函数名中的JSON数据。 jQuery处理幕后的函数名称,并将数据传递给成功处理程序。通过动态创建一个脚本元素来加载数据,其中src属性指向被调用的服务,然后附加到浏览器的DOM。然后浏览器向资源发出请求,Web服务使用回调函数和数据进行响应。

相关问题