我有一個非常簡單的javascript類,通過jquery對我的web服務執行ajax調用。它成功返回數據,但我無法通過我設置數據的變量來檢索它。我不認爲這是ajax調用異步的問題,因爲我已經爲所有ajax事件設置了事件處理程序,但其中一些事件不會觸發。我不知道什麼是錯的。下面是完整的代碼:jQuery Ajax計時問題
的Javascript:
function testClass(){
this.returnData = "";
this.FireAjax = function(){
$.getJSON("http://localhost/mywebapp/webservices/service.asmx/Initialize?userID=12&jsoncallback=?",
function(data){
this.returnData = data.d;
alert(data.d);
}
);
}
}
HTML頁面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://localhost/mywebapp/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="testClass.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var obj = new testClass();
$("#debug").ajaxError(function(event, request, settings){
$(this).append("<b>Ajax Error!</b><br />"); //this does not fire
});
$("#debug").ajaxSend(function(evt, request, settings){
$(this).append("<b>Ajax Send!</b><br />"); //this does not fire!?
});
$("#debug").ajaxStop(function(){
$(this).append("<b>Ajax Stopped</b><br />"); //this fires
});
$("#debug").ajaxComplete(function(event,request, settings){
$(this).append("<b>Ajax Completed!</b><br />"); //this fires
$(this).append("<h2>" + obj.returnData + "</h2>"); //this returns an empty string!!!!!!
});
$("#debug").ajaxSuccess(function(evt, request, settings){
$(this).append("<b>Ajax Successful!</b><br />"); //this fires
});
$("#debug").ajaxStart(function(){
$(this).append("<b>Ajax Started!</b><br />"); //this fires
});
obj.FireAjax();
});
</script>
</head>
<body>
<div id="debug">
</div>
</body>
</html>
附加信息: 如果我刪除了完整的事件在我的HTML頁面,然後將調用obj.returnData在我的停止事件(認爲也許我的HTML完整事件覆蓋我的testClass完整函數),我得到了相同的結果。
是什麼 「這」 指的是在testClass.js匿名函數? – 2009-08-14 19:29:55
「這個」應該指對象的實例......你是否認爲情況並非如此? – 2009-08-14 19:34:51
不知道爲什麼ajaxSend沒有開火,這是一個謎。 – 2009-08-15 12:19:04