jQuery Ajax请求返回错误和状态0

时间:2022-10-08 23:44:36

I use this pseudo-class to make Ajax request to server:

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

This is PHP that will be loaded:

这是将加载的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;
    }

When I instantiate a new object, and I use it to load a request, it always return to me error, and status code is 0. The server correctly generates an XML document. Why I can't get a correct 200 code back?

当我实例化一个新对象,并使用它来加载一个请求时,它总是返回给我错误,状态代码为0.服务器正确生成一个XML文档。为什么我无法获得正确的200代码?

1 个解决方案

#1


2  

If this happens on your second call to require, it's because the call to abort() results in your callback being called with a status code of 0. You should later get the correct result for the second request.

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

In jQuery 1.4 there's also a bug where your success callback is called after an aborted request. See my answer to Request periodically coming back empty under race conditions.

在jQuery 1.4中,还有一个错误,即在中止请求后调用成功回调。请参阅我的回答,请求在竞争条件下定期回来。

Unrelated to this issue, there are variables in your code (timer and ajaxCall) that seem to be referenced both with and without a leading underscore.

与此问题无关,您的代码(timer和ajaxCall)中的变量似乎都带有和不带前导下划线的引用。

#1


2  

If this happens on your second call to require, it's because the call to abort() results in your callback being called with a status code of 0. You should later get the correct result for the second request.

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

In jQuery 1.4 there's also a bug where your success callback is called after an aborted request. See my answer to Request periodically coming back empty under race conditions.

在jQuery 1.4中,还有一个错误,即在中止请求后调用成功回调。请参阅我的回答,请求在竞争条件下定期回来。

Unrelated to this issue, there are variables in your code (timer and ajaxCall) that seem to be referenced both with and without a leading underscore.

与此问题无关,您的代码(timer和ajaxCall)中的变量似乎都带有和不带前导下划线的引用。