Manuel Bitto
Manuel Bitto

Reputation: 5253

jQuery Ajax request returns error and status 0

I use this pseudo-class to make Ajax request to 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 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 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?

Upvotes: 2

Views: 5654

Answers (1)

interjay
interjay

Reputation: 110069

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.

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.

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.

Upvotes: 2

Related Questions