JQuery IE9 JSON.SyntaxError parseerror, but JSON is valid

I have jQuery (1.6.2, the latest stable) code that works in Chrome, Firefox, Safari, IE7 and IE8 ... but IE9 does not parse my ajax json response. Json is valid and I ran it through http://jsonlint.com/

$.ajax({ url: lookupURL, dataType: "json", cache: false, // don't cache the result contentType: "application/json", //tell the server we're looking for json success: function(data) { // do stuff with result }, error: function(xhr, errorString, exception) { alert("xhr.status="+xhr.status+" error="+errorString+" exception=|"+exception+"|"); } }); 

An error handler is a single jQuery call (IE9 only). Xhr.status = 200, errorString = parseerror and exception = SyntaxError JSON.parse

My json IS is valid, and I even checked using ultimately a simple json string:

 {"foo":"bar"} 

I checked using xhr.responseText that there are no leading or trailing spaces on json.

Why does this not work in IE9?

+4
source share
2 answers

Found a problem. The system I work with is a fairly large structure of CMS and E-Commerce, so they have a lot of javascript in their own libraries. Inside one of their js libraries, they replaced the global JSON object and provided their own implementation of JSON.parse. It looks like it was an old and / or hacked version of json2 from json.org. When you tried to solve the problem earlier, I tried to install json2 as a JSON object to no avail ... but it turned out that they then glued their json2 to them. I moved my json2 installation to loading the latest javascript and now it works. I don’t know what only IE9 influenced ... but there you go.

+2
source

A few things are for you to try, but delete the contentType first, as I don't think you need to work out your error.

1) From here: http://api.jquery.com/jQuery.ajax/ dataType "Starting with jQuery 1.5, jQuery can convert the data type from what it got into Content- Enter the header of the type you need." So you can try your dataType as "text json"

2) Is your json fixed (no spaces around it)?

3) Have you tried (at least as a test) with getJSON ()?

+1
source

Source: https://habr.com/ru/post/1368864/


All Articles