2012-08-17 105 views
0

我正在使用ajax(通過jquery)與數據庫交換數據。由於.ajaxcomplete函數總是基於帶有選擇器的jquery對象,還有其他方法可以檢查這個顯式ajax請求是否成功? .ajax不屬於像div等任何特定的dom對象。我想在純Javascript文件中使用Ajax。在這一刻不與特定的html頁面關聯。 (文檔)$ .ajaxComplete()的作品,但不是我想要的jquery ajaxComplete()沒有DOM對象

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]); 
      } 
     } 
    }) 
} 
+0

它看起來並不像你在這裏需要一個全球性的'ajaxComplete'處理程序。您是否嘗試在調用'$ .ajax()'時指定'complete'處理程序? – 2012-08-17 09:51:47

回答

0

當定義你的Ajax調用,您可以指定一個「完整」的回調。在所有ajax調用和成功/錯誤回調之後調用此回調。

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

$ .ajax從jQuery 1.5開始也返回promise對象。如果您在使用jQuery的更高版本,你也可以這樣做:

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

好吧,我會試着用完整的標籤。 thx – simmi91 2012-08-17 10:01:07

+0

使用完全回調的區別在於它現在只針對這個ajax調用。如果頁面上有多個不同的ajax調用,則必須將其添加到它們中的每一個。 – Flater 2012-08-17 10:10:52

1

我傾向於使用ajaxStop超過ajaxComplete。不知道所有的差異,但我認爲它是相似的。

您綁定ajaxComplete的元素並不重要。代碼下面的兩個片段做同樣的:

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

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

所以我覺得你的問題可以通過使用$(document)來解決。

+0

第二次發生在每個ajax調用中,第一次只調用#some_element或不是? – simmi91 2012-08-17 09:59:51

+0

ajaxStop總是爲註冊的每個處理程序觸發。放置文檔或選擇器無關緊要,它會在ajax調用後觸發。 $(document)或$(selector)唯一真正的區別在於,使用'$(this)'來調用函數的上下文是不同的。我從來沒有遇到任何其他根據事件綁定的原始選擇器而改變的東西。 – Flater 2012-08-17 10:02:55

+0

Ajax調用永遠不會被綁定到項目上,因爲沒有任何關於Ajax調用需要它與現有項目相關的信息。 – Flater 2012-08-17 10:04:58

0

您可以爲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/