我正在使用jQuery制作简单的自动提示(autocompleter)插件.不幸的是我必须使用jsonp.没关系,它可以工作,但是当我中止请求时,它会抛出错误.
Uncaught TypeError: Property 'jQuery171036404498340561986_1330693563756' of object [object Window] is not a function@H_403_4@有代码
if(xhr) {xhr.abort()}; xhr = $.ajax({ url: "someurl/getmedata",type: 'get',dataType: 'jsonp',data: {q: "query"},success: function(results) {},error: function() {} })@H_403_4@使用json,xml或其他请求的经典方式可以正常工作. @H_403_4@安妮的建议?
解决方法
JSONP不使用XMLHTTPRequest,但实际上创建了一个< script>文档中的标记,以便能够进行跨域请求.
@H_403_4@您根本无法中止JSONP请求.
@H_403_4@它在$.ajax()文档中说明:
@H_403_4@Some types of Ajax requests,such as JSONP and cross-domain GET requests,do not use XHR; in those cases the XMLHttpRequest and textStatus parameters passed to the callback are undefined.@H_403_4@至于jQuery 1.5,以前是从$.ajax()返回XHR对象,现在它返回一个超集jqXHR对象,它封装了XHR对象.
@H_403_4@When the transport mechanism is something other than XMLHttpRequest (for example,a script tag for a JSONP request) the jqXHR object simulates native XHR functionality where possible.@H_403_4@所以它返回一个对象但实际上没有一些功能,比如.abort(). @H_403_4@插件jQuery-JSONP似乎提供了手动中止JSONP请求的可能性.我自己没有用过,但值得一试.