【发布时间】:2014-06-30 07:28:35
【问题描述】:
你好,我用 jquery mobile 建立了一个网站,我正在使用 jquery mobile 的自动完成我想中止以前的 ajax 请求,但我面临Cannot read property 'abort' of undefined 的错误
这是我的代码:
<div id="myPage">
<ul id="autocomplete" data-role="listview" data-inset="true" data-filter="true" data-filter-placeholder="Find a city..." data-filter-theme="a"></ul>
</div>
这里是js代码:
<script type="text/javascript">
var xhr ;
$(document).on( "pageinit", function() {
autoComplete();
});
function autoComplete(){
$( ".ui-input-text" ).on( "keyup", function ( e, data ) {
var $ul = $('#autocomplete'),
$input = $(this),
value = $input.val(),
html = "";
$ul.html( "" );
if ( value && value.length > 1 ) {
$ul.html( "<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>" );
$ul.listview( "refresh" );
console.log(xhr);
if(xhr && xhr.readystate != 4 && typeof xhr !== 'undefined'){
xhr.abort();
}
xhr = $.ajax({
url: "http://gd.geobytes.com/AutoCompleteCity",
type:'POST',
dataType: "jsonp",
data: {
q: $input.val(),
}
})
.then( function ( response ) {
$ul.html( response );
$ul.listview( "refresh" );
$ul.trigger( "updatelayout");
});
}
});
}
</script>
这里是DEMO 链接:http://dev.artoonsolutions.com/jignesh/mobile_auto/index.html
【问题讨论】:
-
this.xhr.abort();到xhr.abort();。 -
xhr.abort();它也是创建和错误:Uncaught TypeError: undefined is not a function @Omar
-
.abort()是一个函数吗? -
一点也不:.abort() 不是函数。这是方法@Omar
标签: javascript jquery html ajax jquery-mobile