2

我使用这个伪类向服务器发出 Ajax 请求:

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 代码?

4

1 回答 1

2

如果在您第二次调用 时发生这种情况require,那是因为调用abort()导致您的回调被调用,状态码为 0。您稍后应该获得第二次请求的正确结果。

在 jQuery 1.4 中还有一个错误,success在中止请求后调用您的回调。请参阅我对请求在竞争条件下定期返回空的答案

与此问题无关,您的代码 (timerajaxCall) 中有一些变量似乎在有或没有前导下划线的情况下都被引用。

于 2010-04-19T17:33:38.143 回答