简体   繁体   English

jQuery的ajaxComplete()没有DOM对象

[英]jquery ajaxComplete() without dom object

I'm using ajax (via jquery) for exchanging data with a database. 我正在使用ajax(通过jquery)与数据库交换数据。 As the .ajaxcomplete function is always based on a jquery object with selector, is there any other way to check whether this explicit ajax request was sucessfull? 由于.ajaxcomplete函数始终基于带有选择器的jquery对象,是否还有其他方法可以检查此显式ajax请求是否成功? The .ajax does not belong to any specific dom object like div etc. I want to use Ajax in a pure Javascript file. .ajax不属于div等任何特定的dom对象。我想在纯Javascript文件中使用Ajax。 In this moment not associated with the specific html page. 此刻未与特定的html页面关联。 $(document).ajaxComplete() works but is not what I want $(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]);
            }
        }
    })
}

I'm prone to using ajaxStop over ajaxComplete . 我倾向于在ajaxStop上使用ajaxComplete Not sure about all the differences though, I think it's similar. 虽然不确定所有差异,但我认为这是相似的。

The element you bind ajaxComplete to doesn't really matter. 绑定ajaxComplete的元素并不重要。 The following two snippets of code do exactly the same: 以下两个代码段完全相同:

$("#some_element").ajaxComplete(function () {
    alert($(this).html());
});

versus

$(document).ajaxComplete(function () {
    alert($("#some_element").html());
});

So I think your problem can be solved by using $(document) . 所以我认为您的问题可以通过使用$(document)来解决。

When defining your ajax call you can specify a "Complete" callback. 在定义ajax调用时,您可以指定“完成”回调。 This call back gets called after all ajax calls and after success/error callbacks. 在所有ajax调用以及成功/错误回调之后,将调用此回调。

$.ajax({
    complete: function(){ alert('hello'); }
});

$.ajax also returns a promise object as of jQuery 1.5. 从jQuery 1.5开始,$ .ajax还返回一个promise对象。 If you are using a later version of jQuery you could also do this: 如果您使用的是jQuery的更高版本,则也可以这样做:

var jqXhr = $.ajax({ ... your code }).always(function(){ alert('hello'); });
//Or
jqXhr.always(function(){ alert('hello'); });

You can set global event handlers for ajax requests in jQuery. 您可以在jQuery中为ajax请求设置全局事件处理程序。 You can add a dynamic property (say 'id') and identify a particular request. 您可以添加动态属性(例如“ 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";

demo : http://jsfiddle.net/diode/3fX8b/ 演示: http : //jsfiddle.net/diode/3fX8b/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM