簡體   English   中英

jQuery的ajaxComplete()沒有DOM對象

[英]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";

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM