为什么以下jQuery ajax总是调用错误回调,即使Fiddler没有报告错误:
$.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"}])
我实际上不久前就有这个工作,但现在无法弄清楚我做错了什么。
答案 0 :(得分: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
]
答案 1 :(得分: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专家可以在这里发挥更多的作用。