代码之家  ›  专栏  ›  技术社区  ›  Manuel Bitto

jquery ajax请求返回错误和状态0

  •  2
  • Manuel Bitto  · 技术社区  · 16 年前

    我使用这个伪类向服务器发出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码?

    1 回复  |  直到 13 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    如果你第二次打电话给 require ,这是因为 abort() 结果将使用状态代码0调用回调。您稍后应该会得到第二个请求的正确结果。

    在jquery 1.4中还有一个bug success 在中止请求后调用回调。见 my answer to Request periodically coming back empty under race conditions .

    与此问题无关,您的代码中有变量( timer ajaxCall )它们似乎同时被引用,有或没有前导下划线。

    推荐文章