1
var url = "/MyApp/pspace/filter";
var data = JSON.stringify(myData);
$.post(
  url, 
  data,
  function(response, textStatus, jqXHR) {
    console.log("response: " + response);
  },
  "json"
);

In reality, response should be a json string.

In Chrome, response is a string that I can parse with $.parseJSON().

In Firefox, response is an XMLDocument (with a parse error), unless I use dataType: "text". Why?

Tony R
  • 10,116
  • 21
  • 70
  • 100
  • 1
    I don't think this is your actual code, it would throw a syntax error. – Felix Kling Jan 03 '12 at 23:08
  • It would? I must have made a mistake while trimming, do you see it? – Tony R Jan 03 '12 at 23:09
  • `dataType: "json"` should just be `"json"`. I thought JavaScript might interpret `dataType:` as a label, but it throws an error for me. And if you pass `"json"` as last parameter, jQuery would parse the response automatically. Have you checked the response header? – Felix Kling Jan 03 '12 at 23:10
  • Oops, yea it just takes arguments, not an object. Fixed, thank you. – Tony R Jan 03 '12 at 23:11
  • It parses the response as an XMLDocument EVEN with "json". This is my confusion. – Tony R Jan 03 '12 at 23:11
  • 2
    What `Content-Type` is the server responding with? It should be [`application/json`](http://stackoverflow.com/a/477819/15031) for JSON. – Jonathan Lonowski Jan 03 '12 at 23:12
  • 1
    Okay, not to be obvious, but the first place to look would be at the results of the URI. open it with a browser and see if XML is what's being returned. – Charlie Martin Jan 03 '12 at 23:13
  • Okay, it was the content-type coming from the server. Works in Chrome/FF now. I still wonder why Chrome handles this situation fine, where Firefox doesn't. I wouldn't call it an "intelligent guess" if it thinks a string starting with "{" is XML... – Tony R Jan 03 '12 at 23:19

1 Answers1

0

If you are setting "json" as your response type jQuery should parse it into an object for you automatically. If you forgot to tell jQuery what type of response to expect different browsers will treat it differently.

The solution is to make sure you specify your response type as "json" and then of course to make sure the data that is being returned is an actual JSON string.

Jamund Ferguson
  • 15,537
  • 3
  • 40
  • 49