[英]jquery ajaxComplete() without dom object
我正在使用ajax(通过jquery)与数据库交换数据。 由于.ajaxcomplete函数始终基于带有选择器的jquery对象,是否还有其他方法可以检查此显式ajax请求是否成功? .ajax不属于div等任何特定的dom对象。我想在纯Javascript文件中使用Ajax。 此刻未与特定的html页面关联。 $(document).ajaxComplete()可以工作但是不是我想要的
this.replot=function(){
$(this).ajaxComplete(function() {alert('hallo');}); //here is my prob
var that=this;
var anfrage='anfrage= SELECT '+ this.xvaluecol+', '+this.y1valuecol+ ' FROM '+ this.tablename+ ' WHERE '+this.xvaluecol+' <=\'2010-11-06 15:00:00\' AND '+this.xvaluecol+' >=\'2010-11-06 07:00:00\'';
$.ajax({
url : 'getdata.php',
dataType : 'json',
data: anfrage,
type : 'post',
success : function(json) {
if(String(json[0][0]).search('error')==-1)
{
that.data1=json;
that.xaxismin=json[0][0];
that.xaxismax=json[json.length-1][0];
that.yaxsismin=parseInt(that.find_min(json));
that.yaxismax=parseInt(that.find_max(json));
console.log(json);
console.log("yaxismin="+that.yaxismin);
console.log("yaxismax="+that.yaxismax);
//c=new Date((that.xaxismin));
//c.setMinutes(c.getMinutes()+1441+60);
//(c.toLocaleString());
that.update();
$.jqplot(that.divid,[that.data1,that.data2],that.options).replot();
}
else
{
alert('Plot nicht moeglich Fehlercode: '+json[0][1]);
}
}
})
}
我倾向于在ajaxStop
上使用ajaxComplete
。 虽然不确定所有差异,但我认为这是相似的。
绑定ajaxComplete
的元素并不重要。 以下两个代码段完全相同:
$("#some_element").ajaxComplete(function () {
alert($(this).html());
});
与
$(document).ajaxComplete(function () {
alert($("#some_element").html());
});
所以我认为您的问题可以通过使用$(document)
来解决。
在定义ajax调用时,您可以指定“完成”回调。 在所有ajax调用以及成功/错误回调之后,将调用此回调。
$.ajax({
complete: function(){ alert('hello'); }
});
从jQuery 1.5开始,$ .ajax还返回一个promise对象。 如果您使用的是jQuery的更高版本,则也可以这样做:
var jqXhr = $.ajax({ ... your code }).always(function(){ alert('hello'); });
//Or
jqXhr.always(function(){ alert('hello'); });
您可以在jQuery中为ajax请求设置全局事件处理程序。 您可以添加动态属性(例如“ id”)并标识特定请求。
$.ajaxSetup({
success: function(data, textStatus, jqXHR) {
console.log(jqXHR.id + " success");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.id + " failed");
},
complete: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR.id + " completed");
}
});
$.ajax({
url: "/"
}).id = "request1";
$.ajax({
url: "/"
}).id = "request2";
$.ajax({
url: "sdddqwdqwdqwd.com"
}).id = "request3";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.