0

I have a few very simple AJAX calls using jQuery.
My c# webmethods are marked as [ScriptMethod(ResponseFormat = ResponseFormat.Json)] and return a correctly-formatted JSON.
This works great in FF, but goes crazy on IE:
the error handler is invoked, where the response has status = 200 (!!), the responseText is the valid JSON I want to get, and the statusText is "parsererror".

I saw somewhere on SO (can't seem to find it again now) that the problem is that IE expects an xml format response, and throws parser exception on anything else.
the suggested solution was, basically, using the error handler as a success handler:

$.ajax(
            {
                url: "SampleScriptService.asmx/Function",
                type: "POST",
                data: {},
                contentType: "application/json;",
                dataType: "json",
                success: function (result) {
                    SuccesFunction(result.d);
                },
                error: function (e) {
                    if (e.status != 200) {
                        alert("this is a real error");
                    }
                    SuccesFunction(JSON.parse(e.responseText).d);
                }
            });

this seems really hacky to me. Does anybody know of any other solution?
p.s. things i've already tried: these, this, all solutions suggested here

UPDATE
not sure whether it's related or not, but IE also gives this strange error-

Expected ';' Line: 101139813 Char: 5 Code: 0 URI: http://localhost/sample/samplePage.htm

needless to say, my htm file contains ~100 lines.
If I make no ajax calls (and therefore- the above error handler isn't called) then this error does not appear.

Community
  • 1
  • 1
J. Ed
  • 6,646
  • 4
  • 36
  • 52
  • Not sure what the suggestions you tried were, but there's an issue in that your `contentType` is JSON, but your data is an empty string. Try `data: "{}",` to make it an empty JSON object. – lonesomeday May 15 '11 at 12:03
  • @lonesomeday: tried it; still the same. – J. Ed May 15 '11 at 12:28

2 Answers2

1

If this only happening on IE then it is because of a typo in your response. Please show the json you got from the server. You have an object where the last item has a trialling comma.

Like this

 { "l1" : 1, "l2" : 2, }

See how this ends with , }? It need to end with just }.

Just to confirm, the json looks like this with no surrounding "?

 {"d":
    {"__type":"PresentationObjects.UserPO",
     "Username":"admin",
     "FullName":"Admin User",
     "Password":"",
     "Roles":15,
     "UserRolesStrings": ["monitor","reports","schedule"­,"administration"],
     "IsAdministrator":true,
     "IsMonitor":true,
     "IsSchedule":true,
     "IsR­eports":true
    }
 }

I see that it says roles is 15 but you only show 4. Did you edit the results?

Hogan
  • 63,843
  • 10
  • 75
  • 106
  • This is your problem. You are sending no data. Send something at least. – Hogan May 15 '11 at 12:23
  • nope. doesn't work also for non-null values: "{"d":{"__type":"PresentationObjects.UserPO","Username":"admin","FullName":"Admin User","Password":"","Roles":15,"UserRolesStrings":["monitor","reports","schedule","administration"],"IsAdministrator":true,"IsMonitor":true,"IsSchedule":true,"IsReports":true}}" – J. Ed May 15 '11 at 12:31
0

OK, this is totaly my bad. I upgraded from jQuery 1.4.4 to jQuery 1.6 and didn't notice that my pages actually had problems on ALL browsers.
the problem was with the validation plugin, as described in this question.
thanks to all who took the time to answer.

Community
  • 1
  • 1
J. Ed
  • 6,646
  • 4
  • 36
  • 52