原型ajax statusText在Internet Explorer上为空

时间:2010-07-20 18:53:49

标签: javascript ajax prototypejs

Firefox中responseText为空,但在Internet Explorer中可以。 我还会在返回客户端之前记录响应,以便在那里看到我的回复。

这是我的要求,我添加了一个setTimeout,但这也无济于事。

var ajaxUrl = "./ajaxHandlers/ajax-handler.php";

var myAjax = new Ajax.Request(
  ajaxUrl,
  {
    method: 'post', 
    parameters: params, 
    onComplete: function(response)
    { 
      setTimeout(handleResponse(response,callback) ,5000);
    } 
 });



 function handleResponse(response,callback)
 {
  alert(response.responseText);

  try
  {
     eval("var r = " + response.responseText);
  }
  catch (e)
  {
     alert("EXCEPTION = " + e.constructor);

     showError("error evaluating response : Response text:<br/>" + response.responseText);
     var r = new Object();
     r.message = 'Error evaluating response';
     r.status = 'error';
     if (typeof callback == 'function') callback(r);
     return;
  }
}

1 个答案:

答案 0 :(得分:1)

在onComplete中,您的回调变量未定义,因此会导致您的问题:

onComplete: function(response)
{
      handleResponse(response);
} 

onComplete: function(response, callback)
{
      handleResponse(response, callback);
} 

编辑: 这是我的完整代码。它在FF 3.5.10上工作绝对正常..嗯不是最新的但应该是一样的。由于console.info输出语句的顺序,我可以确认ajax是同步的:

var ajaxUrl = "./ajaxHandlers/ajax-handler.php";
console.info('1');

var myAjax = new Ajax.Request(
  ajaxUrl,
  {
    method: 'post',
    asynchronous: false, 
    parameters: {}, 
    onComplete: function(response, callback)
    {
      handleResponse(response,callback);
    } 
   }
 );

console.info('2');

 function handleResponse(response, callback)
 {
 console.info(response.responseText);

  try
  {
     eval("var r = " + response.responseText);
  }
  catch (e)
  {
     alert("EXCEPTION = " + e.constructor);

     showError("error evaluating response : Response text:<br/>" + response.responseText);
     var r = new Object();
     r.message = 'Error evaluating response';
     r.status = 'error';
     if (typeof callback == 'function') callback(r);
     return;
  }
}