JSON parsing error in Internet Explorer

I am using jscript to retrieve JSON data from Flickr. Works 100% in every browser except IE.
I use jquery for every function that calls this specific function for IE:

//some code if ($.browser.msie && window.XDomainRequest) { var xdr; var url = "http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=" + apiKey + "&photoset_id=" + set + "&extras=url_sq&format=json&nojsoncallback=1"; xdr = new XDomainRequest(); if (xdr) { xdr.open("get", url); xdr.send(); var data = JSON.parse(xdr.responseText); //some jquery stuff } } 

In IE, the function returns a syntax error in var data = JSON.parse (xdr.responseText); but the error is random, it retrieves a random number of photos before showing the error ..

I checked all the variables involved and everything returns OK.

I am using json2.js

UPDATE:

Possible JSON results:

 { "photoset": { "id": "72157627083924637", "primary": "5943107169", "owner": " 63570294@N03 ", "ownername": "motorespt.com", "photo": [ { "id": "5943107169", "secret": "e6099e3936", "server": "6029", "farm": 7, "title": "Peugeot 206", "isprimary": "0", "url_sq": "http://farm7.static.flickr.com/6029/5943107169_e6099e3936_s.jpg", "height_sq": 75, "width_sq": 75 } ], "page": 1, "per_page": 500, "perpage": 500, "pages": 1, "total": "1" }, "stat": "ok" } 

or

 {"stat":"fail", "code":1, "message":"Photoset not found"} 

UPDATE:
thanks to all my help, I was able to find the error and make the function compatible with IE 7+, Firefox, Chrome, etc.

 function flickr_test(){ var apiKey = 'YOUR_API_KEY'; $.ajax({ url: 'http://api.flickr.com/services/rest/', data: { method: 'flickr.test.echo', api_key: apiKey, format: 'json', test: 'test string', jsoncallback: 'jsonFlickrApi' }, dataType: 'jsonp' }); } function jsonFlickrApi(response){ console.log(response.stat); } 

PS: "test" var is the string I wanted to pass to the callback function

+2
source share
4 answers

JSON parsing on IE 8 and below has problems. It cannot identify JSON functions.

Download the file https://github.com/douglascrockford/JSON-js/blob/master/json2.js Include it in your application and it should fix the problem.

+8
source

When using different browsers, you can choose a different method:

select eval in IE6, 7 select native JSON in IE8 select a new function in Firefox2, 3 select eval in Safari4 eval has the same performance as the new function in general when you use other browsers.

+1
source

Using the file https://github.com/douglascrockford/JSON-js/blob/master/json2.js you will have this error: "parsing line: line: 191 Error: object does not support this property or method". the property or method is valueOf ().

I am using the solution suggested in JSON2 Error / Conflict with another script

return ((typeof this.valueOf === 'function')? this.valueOf (): this.toString ());
instead of return this.valueOf ();

0
source

If you download an iframe, IE10 will accept a URL length of 4098.

0
source

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


All Articles