0

I used jQuery's .ajax function like:

  $.ajax({
type:"POST",
url:"./index.php",
data:{ajax_fetch:1,action:"fetch_devInfo",nwType:nw_type,devTSN:dev_tsn,devSTime:dev_sTime,devETime:dev_eTime,dev_gType:dev_graphType},
dataType:"xml",
error:errHandler,
success:function(xml,textStatus)
 {
  xml_process(xml,textStatus,$("div#cont-Dev"),"Device");
 }  
});

     // function for .ajax error callback  
 function errHandler(xhr, statusText, error)
{
  if (xhr.status == "0" && statusText == "error")
  {
    $("body").append('<div class="ui-state-error ui-corner-all" id="error">Network down, try again later</div>');   
  }
  else if (xhr.status == "200" && statusText == "parseerror")
  {
    window.location="./login.php";
  }  
}

My assumption is: if .ajax success, then the server must return it a XML file (identified by its header header("Content-type: text/xml")), that's why I specify the dataType as "xml"; however, if it failed (for example: session time out), index.php will redirect user to the login.php. In that case, the response is some HTML, shouldn't .ajax go to the function errHandler? Why does it always go to the success handler?

Sumurai8
  • 20,333
  • 11
  • 66
  • 100
WilliamLou
  • 1,914
  • 6
  • 27
  • 38
  • http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call – John Hoven Dec 11 '09 at 19:07
  • I saw that thread before, I don't think it solves my problem here. No matter what the server's response (XML or HTML), the status code is always 200 (because it got successful response). My point is: since I specify the dataType: "xml", I remember .ajax will generate a 'parseerror' when the response is actually HTML, and then it will go to the errorHandler directly, isn't that true? – WilliamLou Dec 11 '09 at 19:24

2 Answers2

3

jQuery's $.ajax function takes the datatype and attempts to use the response as if it's that datatype, mostly to make things easier for you when you start using the response data. However, that's not how jQuery defines success or failure with the success or error handlers.

In this case, 'success' is defined by receiving information from the server. If information is received by the server, the request succeeded. Afterwards, jQuery tries to parse the information as XML (in your case). However, it isn't (or isn't what you expect), so it won't correctly do what you want it to.

Using that, I would rewrite the success handler to deal with XML or HTML data from the server, and use the error handler for your first error, where the server is down, etc.

Jeff Rupert
  • 4,690
  • 2
  • 20
  • 17
  • I agree with you. That's correct based on what I have now. It seems like that I have to differentiate XML and HTML in 'success' handler myself. Then my question is: do you know if there is an easy way to tell xml and html apart? my HTML response will always starts with " " – WilliamLou Dec 11 '09 at 19:47
  • You could do a simple regular expression match on it. I'm not sure exactly the syntax in JavaScript off the top of my head, but you could use this expression: `/^ – Jeff Rupert Dec 11 '09 at 20:25
  • My personal solution would be to send back an array of info using JSON. Then you could just make an associative array in PHP and send it back. `return json_encode( $ret_info );` Then, in the JavaScript, you could just check `"xml" === response.type` or whatnot. – Jeff Rupert Dec 11 '09 at 20:27
2

I couldn't see a definitive answer in the jquery docs. But I'm thinking the data for the 302 response (or whatever) IS valid XML data (thus could be parsed) even if HTML isn't always.

HTTP/1.1 302 Found
Cache-Control: no-cache
Keep-Alive: timeout=3, max=993
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: -1
Location: /Login.aspx
Server: Microsoft-IIS/7.0
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Date: Fri, 11 Dec 2009 19:17:27 GMT
Content-Length: 139

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/Login.aspx">here</a>.</h2>
</body></html>
John Hoven
  • 4,085
  • 2
  • 28
  • 32
  • +1 You won't see the parser error because it is well formed, you won't see the timeout because you did get a response. Check for the response == 200 in your sucessHandler, and call errHandler if it fails. – ryanday Dec 11 '09 at 19:30
  • actually, "redirect" may confuse you. What I have here is not a redirect: the ajax request is always sent back to index.php, which contains a session verification part. If the session is not verified, index.php will generate a HTML login page for the user. In that case, the server doesn't send back an explicit HTML 'redirect' header (code:3xx etc..) – WilliamLou Dec 11 '09 at 19:32
  • OK, I was confused a bit. +1 to Jeff then. – John Hoven Dec 11 '09 at 19:43