views:

260

answers:

2

hi all: 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 errHanlder? why is it always go to the success handler? I don't know if I explained the problem clearly. Thank you!

+2  A: 

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>
benjynito
+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
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
OK, I was confused a bit. +1 to Jeff then.
benjynito
+3  A: 

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
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 "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link href="./style/login.css" rel="stylesheet" type="text/css" media="screen" />"
WilliamLou
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: `/^<!DOCTYPE html/` or `XHTML 1.1`. Both of those would define an HTML page, and then you could easily differentiate.
Jeff Rupert
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