JQuery Ajax request return error and status 0

I am using this pseudo class to make an Ajax request to the server:

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 the PHP that will be loaded:

<?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 use it to load the request, it always returns me an error and the status code is 0. The server is generating the XML document correctly. Why can't I get the correct 200 code?

+2


a source to share


1 answer


If this happens on the second call require

, it is because the call abort()

results in your callback being called with a status code of 0. You should later get the correct result for the second request.

There is also a bug in jQuery 1.4 where a callback is called success

after an aborted request. See my answer to a request that periodically returns to a race condition .



Not related to this problem, in your code, there are some variables ( timer

and ajaxCall

) that are referenced by both with and without the underscore.

+2


a source







All Articles