1

为什么即使 Fiddler 没有报告错误,下面的 jQuery ajax 也总是调用错误回调:

 $.ajax({
        type: 'GET',
        url: "http://open.mapquestapi.com/nominatim/v1/search?format=json&viewbox=-122%2C46%2C-73%2C40&countrycodes=ca&bounded=1&addressdetails=0&q=[hotel]",
        dataType: 'jsonp',
        jsonp: 'json_callback',
        error: function(parameters) {
            alert(parameters);
        },
        success: searchCompleteCallBack
    });

这是 Fiddler 会话,虽然成功,但有一个带有横线的红色圆圈作为状态图标,如下所示: 在此处输入图像描述

GET /nominatim/v1/search?format=json&viewbox=-122%2C46%2C-73%2C40&countrycodes=ca&bounded=1&addressdetails=0&q=[hotel]&json_callback=jQuery17108850838504386628_1334974646679&_=1334974661295 HTTP/1.1
Accept: application/javascript, */*;q=0.8
Referer: http://localhost:50383/?AspxAutoDetectCookieSupport=1
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; BOIE9;ENGB)
Accept-Encoding: gzip, deflate
Host: open.mapquestapi.com
Connection: Keep-Alive


HTTP/1.1 200 OK
Date: Sat, 21 Apr 2012 02:17:41 GMT
Server: Apache
Content-Location: search.php
Vary: negotiate
TCN: choice
Access-Control-Allow-Origin: *
Content-Length: 4775
Keep-Alive: timeout=5, max=91
Connection: Keep-Alive
Content-Type: application/json; charset=UTF-8

jQuery17108850838504386628_1334974646679([{"place_id":"2166467243","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1417990074","boundingbox":["43.0738813782","43.0938851929","-79.0912683105","-79.0712606812"],"lat":"43.0838817","lon":"-79.0812662","display_name":"Quality Hotel, Murray Street, Niagara Falls, Ontario, Ontario, Canada","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2085733959","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"97928121","boundingbox":["49.8510055541992","49.8513870239258","-97.1532821655273","-97.1526718139648"],"lat":"49.8512060913871","lon":"-97.1530114466667","display_name":"Pembina Hotel, 1011, Pembina Highway, Point Road, Winnipeg, Manitoba, Canada","class":"amenity","type":"hotel"},{"place_id":"2152638544","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1324405899","boundingbox":["43.8393080139","43.8593118286","-79.3921792603","-79.3721716309"],"lat":"43.849309","lon":"-79.382178","display_name":"Holiday Inn Express & Suites, Fulton Way, Thornlea, Markham, Ontario","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2177668535","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"138157978","boundingbox":["43.4092330932617","43.4098968505859","-80.3297119140625","-80.3289260864258"],"lat":"43.40953956119","lon":"-80.3293440133707","display_name":"Hespeler Road, Cambridge, Ontario, Canada","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2138545308","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1280026038","boundingbox":["43.6322805786","43.6522843933","-79.4218270874","-79.401819458"],"lat":"43.6422834","lon":"-79.4118234","display_name":"Palace Arms, Strachan Avenue, Liberty Village, Toronto, Ontario, M5A1Z9, Canada","class":"tourism","type":"hostel"},{"place_id":"5773486","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"583902387","boundingbox":["45.3422987366","45.3623025513","-75.6427056885","-75.6226980591"],"lat":"45.3522996","lon":"-75.6327046","display_name":"Bank Street, Blossom Park, Ottawa, Ontario","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2170250899","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1445914835","boundingbox":["43.1746237183","43.194627533","-79.5842340088","-79.5642263794"],"lat":"43.1846249","lon":"-79.5742298","display_name":"Beamer Falls Manor at Falconridge Farm, Ridge Road West, Grimsby, Ontario, Canada","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2664364","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"343055090","boundingbox":["43.6355078125","43.6555116272","-79.4053170776","-79.3853094482"],"lat":"43.6455088","lon":"-79.3953127","display_name":"Global Village Backpackers Hostel, King Street West, Fashion District, Toronto, Ontario, M5A1Z9, Canada","class":"tourism","type":"hostel"},{"place_id":"2168185190","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"way","osm_id":"129431482","boundingbox":["45.3853416442871","45.3861045837402","-75.733268737793","-75.7327346801758"],"lat":"45.3857092561038","lon":"-75.7330025913023","display_name":"Best Western Macies Hotel, Carling Avenue, Rideauview, Ottawa, Ontario, Canada","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"},{"place_id":"2166467244","licence":"Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.","osm_type":"node","osm_id":"1417990095","boundingbox":["43.0724356079","43.0924394226","-79.0928704834","-79.072862854"],"lat":"43.0824382","lon":"-79.0828672","display_name":"Hilton Niagara Falls, Fallsview Boulevard, Niagara Falls, Ontario, Ontario, Canada","class":"tourism","type":"hotel","icon":"http://open.mapquestapi.com/nominatim/v1/images/mapicons/accommodation_hotel2.p.20.png"}]) 

实际上,我不久前就进行了这项工作,但现在无法弄清楚我做错了什么。

4

2 回答 2

0

我无法在 Firefox 和 Chrome 中重现该问题。

在您的控制台中尝试这段代码,看看发生了什么。

$.ajax({
        type: 'GET',
        url: "http://open.mapquestapi.com/nominatim/v1/search?format=json&viewbox=-122%2C46%2C-73%2C40&countrycodes=ca&bounded=1&addressdetails=0&q=[hotel]",
        dataType: 'jsonp',
        jsonp: 'json_callback',
        error: function(parameters) {
            console.error("error");
            console.error(parameters);
        },
        success: function(parameters) {
            console.log("success");
            console.log(parameters);
        },
    });

我得到了这个:

XMLHttpRequest
success
[
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
, 
Object
]
于 2012-04-21T02:55:42.373 回答
0

在 jQuery-1.7.2.js 中,从加载动态 jsonp 请求脚本(大约 8123 行)到从服务器接收到响应并调用安装在 8016 行的回调时,存在一个灰色区域。看起来这个回调,即使在窗口对象的全局范围内定义,在某些情况下也不存在。此窗口对象下的子元素布局确实与它有关,因为通过单击直接放置在 ASP.NET 剃须刀局部视图上的复选框来调用 ajax 有效,但是当从 OpenLayers.Popup.FrameCloud 中的按钮调用时,您会遇到这个问题。

一种解决方法是防止 jQuery 将随机函数定义为 jsonp 回调,如下所示:

$.ajax({
        type: 'GET',
        url: "http://open.mapquestapi.com/nominatim/v1/search?format=json&viewbox=-122%2C46%2C-73%2C40&countrycodes=ca&bounded=1&addressdetails=0&q=[hotel]&json_callback=onSearchComplete",
        dataType: 'jsonp',
        jsonp: false,
        error: function(parameters) {
            alert(parameters);
        }
    });

在这种情况下,“onSearchComplete”是脚本中定义的全局函数。使用此设置,无论如何调用 ajax 查询,将始终在成功的服务器响应时调用 onSearchComplete。这种情况下的问题是 jQuery 仍然创建和安装随机命名的回调函数,即使它没有作为 json 请求的一部分发送。结果,随机生成的回调将永远不会被执行,并导致 jQuery 将其视为错误条件。

也许 jQuery 大师可以在这里提供更多的信息。

于 2012-05-01T17:40:45.607 回答