2010-04-19 98 views
2

我用這個僞類,使Ajax請求到服務器:jQuery的Ajax請求返回的錯誤和狀態0

function RequestManager(url, params, success, error){ 
    //Save this Ajax configuration 
    this._ajaxCall = null; 
    this._url= url; 
    this._type = params.type; 
    this._success = function(){ 
     alert("ok"); 
    }; 
    this._error = function(){ 
     alert("ko"); 
    }; 
} 

RequestManager.prototype = { 
    require : function(text){ 
     var self = this; 
     this._ajaxCall = $.ajax({ 
      url: this._url, 
      type: this._type, 
      data: text, 
      success: function(xmlResponse){ 
       var responseArray = []; 
       var response = _extractElements(xmlResponse, arrayResponse); 
       self._success(response); 
      }, 
      error: self._error, 
      complete : function(xhr, statusText){ 
       alert(xhr.status); 
       return null; 
      } 
     }); 
    } 

這是一個將被載入PHP:

<?php 
    header('Content-type: text/xml'); 

    //Do something 
    $done = true; 

    $response = buildXML($done); 
    $xmlString = $response->saveXML(); 
    echo $xmlString; 

    function buildXML ($done){ 
     $response = new SimpleXMLElement("<response></response>"); 
     if($done){ 
      $response->addChild('outcome','ok'); 
     } 
     else { 
      $response->addChild('outcome', 'ko'); 
     } 
     return $response; 
    } 

當我實例化一個新的對象,我用它來加載請求,它總是返回給我錯誤,並且狀態代碼是0.服務器正確地生成一個XML文檔。爲什麼我無法獲得正確的200代碼?

回答

2

如果第二次調用require時發生這種情況,這是因爲調用abort()會導致您的回調被調用時的狀態代碼爲0.您應該稍後爲第二個請求獲得正確的結果。

在jQuery 1.4中還有一個錯誤,其中您的success回調是在中止請求後調用的。見my answer to Request periodically coming back empty under race conditions

與此問題無關,您的代碼中有變量(timerajaxCall),似乎在引用下劃線和不帶引號的情況下都被引用。

+0

好的,問題出在另一個代碼塊,無論如何你的信息是有用的。謝謝 – 2010-04-19 19:57:58

+1

問題是什麼? – katrin 2012-09-06 09:23:19