tags:

views:

58

answers:

1

Why is the callback function receiving a XMLHttpRequest object instead of a JSON object?

<%@ Page language='C#' %>
<% 
   if (Request.RequestType == "POST") {
      System.Threading.Thread.Sleep(1000);
      int c;
      if (Session["c"] == null)
         c = 1;
      else
         c = (int)Session["c"] + 1;
      Session["c"] = c;
      Response.ContentType = "application/json";
      Response.Write("{\"mike\": " + c.ToString() + "}");
      Response.End();
   }
%>
<html>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function callback(data,s) {
   if (s=="success")
      $("h1").text(data.responseText);
   else
      $("h1").text(s);
   setTimeout(updateUI,0);
}
function updateUI () {
   $.ajax({ url: "timer.aspx",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            timeout: 5000,
            type: "POST",
            data: "{}",
            complete: callback});
}
$(function() {
   setTimeout(updateUI,0);
});
</script>
<body>
<h1/>
</body>
</html>
+1  A: 

If you read the jQuery API, it says the first argument is the XMLHttpRequest object.

You probably want to use the success callback option in $.ajax instead if you want the JSON object.

jay_soo