views:

367

answers:

4

I have some code on the client that calls an ashx handler using $.ajax() and expects json data from the server. Everything works fine on FF, IE 6,7,8 when I run the application on a local webserver. However, when I deploy the application to a remote test server, IEs stopped working ($.ajax returns a parsererror), while FF continues to work as expected.

My first thought was that my json object must have a trialing comma which IEs hate, but that wasn't the issue as there were no trialing commas. Then, I tried changing various things like the content types from app/json to tex/plain, still the same error.

Something that I found odd is that if I fire up fiddler, then IEs will work remotely, otherwise, I get the parsererror.

Has anyone experienced something like this before? Thanks.

$.ajax({
        type: "GET",
        url: "handlers/GetAsyncResults.ashx",
        contentType: "application/json; charset=utf-8",
        data: {'from': dateFrom, 'to': dateTo, 'accountId' : aId, 'page': currentPage, 'sortField' : sortField, 'sortDirection' : sortDirection},
        dataType: "json",
        success: function(data) { GetAsyncResultsEnd(data); },
        error: function(x, y, z) { GetAsyncResultsErrorHandler(x, y, z); }
    });

EDIT: added code snippet.

A: 

The only thing that has given me a parse error before it invalid JSON. You said that you checked for trailing spaces, but try running your json through - JSON Lint. That has worked well for me. It is interesting though that fiddler helps.

Brian
Just tried Json Lint, my jsons are valid according to the site. Thanks.
ICodeWith.Net
Two things - can you post an example of your JSON response? And I haven't used JQuery's json type. I always took the text and ran it through Crockfords JSON.parse. It would be interesting to see if the issue persists when using that method - but maybe JQuery is using his JSON parser.
Brian
A: 

Try using

contentType: 'application/json',
ChaosPandion
Tried that, still the same error in IE. Thanks.
ICodeWith.Net
+1  A: 

It's sorted. Thanks.

On the server end, after we called context.Response.Write('Our json data'), we then call context.Response.Flush() and context.Response.Close(). After we removed the .Flush() and .Close(), everything started working again. But I still can't explain why it's working for firefox and not IE, nor how fiddler magically made it works for IE.

Thank you.

ICodeWith.Net
A: 

Have you tried simply removing the datatype: "json"

I had this same problem and removing that fixed it. Removing that allows jQuery to "intelligently pass either responseXML or responseText to your success callback".

Mike Fielden