Ho il seguente codice, che mi sta dando un messaggio Method POST, Status (canceled)
di errore:metodo POST, messaggio Stato (annullato) errore
$(document).ready(function() {
var xhr = false;
get_default();
$('#txt1').keyup(function() {
if(xhr && xhr.readyState != 4){
alert("abort");
xhr.abort();
}
if ($("#txt1").val().length >= 2) {
get_data($("#txt1").val());
} else {
get_default();
}
});
function get_data(phrase) {
xhr = $.ajax({
type: 'POST',
url: 'http://intranet/webservices.asmx/GetData',
data: '{phrase: "' + phrase + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(results) {
$("#div1").empty();
if(results.d[0]) {
$.each(results.d, function(index, result) {
$("#div1").append(result.Col1 + ' ' + result.Col2 + '<br />');
});
} else {
alert("no data available message goes here");
}
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message) ;
}
});
}
function get_default() {
$('#div1').empty().append("default content goes here.");
}
});
Il codice effettivamente funziona a patto che ogni richiesta AJAX completa, ma se devo scrivere velocemente in txt1
, ovvero digita il carattere successivo prima che la richiesta precedente termini, ricevo il messaggio di errore Method POST, Status (canceled)
.
Qualcuno sa perché questo sta accadendo e come correggere l'errore?
Questo può essere utile, dare un'occhiata [stackoverflow.com/questions/6678467](http://stackoverflow.com/questions/6678467/ajax-sent-on-keyup-duplicates-results-when-fast-typing) – safarov
Oleg ha probabilmente la migliore risposta qui. La sua correzione dovrebbe aiutarti ad andare oltre con la sceneggiatura. TUTTAVIA: leggendo il tuo codice vedo che non stai creando il codice ideale per questo problema. Creerò una risposta per te. –