0

My jQuery ajax call is failing with an undefined error. My js code looks like this:

$.ajax({
   type: "POST",
   url: "Data/RealTime.ashx",
   data: "{}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   timeout: 15000,
   dataFilter: function(data, type) {
       alert("RAW DATA: " + data + ", TYPE: "+ type);
       return data;
   },
   error: function(xhr, textStatus, errorThrown) {
       alert("FAIL: " + xhr + " " + textStatus + " " + errorThrown);
   },
   success: function(data) {
       alert("SUCCESS");
   }
});

My ajax source is a generic ASP.NET handler:

[WebService(Namespace = "http://my.website.com")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RealTime : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.Write("{ data: [1,2,3] }");
        context.Response.End();
    }

    public bool IsReusable
    { get { return false; } }
}

Now, if I return an empty object ("{ }") in my handler, the call will succeed. But when I return any other JSON object, the call fails.

The dataFilter handler shows that I am receiving a correct object. Firebug shows the response as expected, and the JSON tab shows that the object is parsed correctly.

So what could be the cause?

[Edit] I should have actually written "when I return any invalid JSON object, the call fails"! :D

vgru
  • 49,838
  • 16
  • 120
  • 201
  • 2
    What you have is not valid JSON, what do you get when you change it to this: `context.Response.Write("{ \"data\": [1,2,3] }");` ? – Nick Craver Jun 05 '10 at 10:42
  • Thanks a lot, that did it. Firebug is obviously a bit less strict on parsing it, so I didn't think about it at all. You could copy this into an answer so I can accept it. – vgru Jun 05 '10 at 11:03
  • Firebug isn't parsing *JSON*, it's parsing *Javascript* - there's a difference. If you had used Firebug to pass that string into a Javascript implementation of a strict JSON parser, you would have gotten errors. – Pointy Jun 05 '10 at 11:06
  • @Pointy: Now it makes sense, but the Firebug tab displaying parsed object *is* actually named "JSON". – vgru Jun 05 '10 at 11:11
  • Done :) Glad that solved it for you – Nick Craver Jun 05 '10 at 11:20

1 Answers1

2

You need valid JSON! :)

Change this line:

context.Response.Write("{ data: [1,2,3] }");

To this:

context.Response.Write("{ \"data\": [1,2,3] }");

jQuery 1.4+ doesn't tolerate invalid JSON like it used to (fails silently/in weird ways), so just add the double quotes and you're all set. For a handy tool to test JSON validity, checkout JSONLint: http://www.jsonlint.com/

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Yes, last time I used this stuff was with jQuery 1.3.2, and it worked. It's funny it never occurred to me to try the old project and see what happens. :) – vgru Jun 05 '10 at 11:34
  • @Groo - Ah yeah that's the most common problem with these, but as @Pointy says they're more strict...but for a reason, it's so if the native `JSON.parse` function is present, we can use the browser's much faster implementation to do the work, win for everybody once all browsers support it property, jQuery is just wrapping it until then :) – Nick Craver Jun 05 '10 at 11:58