views:

240

answers:

1

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. Server correctly generates an xml document. Why i can't get a correct 200 code back?

A: 

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 here.

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.

interjay
Ok, the problem was in another block of code, anyway your info are useful. Thanks
Manuel Bitto