之所以来找您,是因为我正在优化我在学习时间内创建的网站,并且我发现我的搜索引擎有时会出现“ ajax call error”错误。 我仍从Ajax开始,但我不明白为什么会这样:/
代码如下:
//Timer - Une seule requête après saisie
var attente = function(){
var timer = 1000;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
}();
$( document ).ready(function() {
$("#autocompl").keyup(function(){
var profileSearch = $(this).val();
if ( profileSearch.length >= 2 ) {
attente(function(){
$.ajax({
type: "POST",
url: "/profile/search/ajax/" + profileSearch,
dataType: 'json',
timeout: 10000,
// Fonction TIMER For no AJAX CALL ERROR
success: function(response){
var profileSearchs = response;
if (profileSearchs.length == 0) {
$('#result-auto-solut').html('No result');
} else {
html = "<ul id='solut' class='auto-complete-list'>";
for (var i = 0; i < profileSearchs.length; i++) {
html += "<li><a href='/structure/"+profileSearchs[i].id+"'><b>"+ profileSearchs[i].name + "</b> <small><i>"+profileSearchs[i].city+" "+ profileSearchs[i].postal+"</i></small></a></li>";
}
html += "</ul>";
$('#result-auto-solut').html(html);
$('#result-auto-solut').find('li').on('click', function () {
$('#result-auto-solut').html('');
});
}
},
error: function() {
$('#result-auto-solut').text('Ajax call error');
}
});
}, 1000 );
} else {
$('#result-auto-solut').html('');
}
});
});
在此先感谢您,并会尽快答复您