JQuery, ajax request failed with JSON in IE

I made an AJAX call and it works on FF and Chrome, but not on IE 7-8-9. I am loading a JSON file from my domain:

$.ajax({ url: 'js/jquery.desobbcode.json', dataType: 'json', cache: false, success: function(json) { alert('ok'); }, error: function(xhr, errorString, exception) { alert("xhr.status="+xhr.status+" error="+errorString+" exception="+exception); } }); 

I also tried adding contentType: 'application/json' , but I get the same output as:

 xhr.status=200 error=parsererror exception=SyntaxError Unterminated string constant 

I checked my JSON file with JSONLint and everything is fine. I checked if there is an extra comma and the contents are also clipped. See my JSON file

If I put dataType: 'text' , I get an OK warning, but a debug popup too.

could you help me? Best wishes.

+4
source share
4 answers

IE is known to have problems with implied content types.

... the new XmlHttpRequest class in Internet Explorer 7 does not implement setRequestHeader very intuitively. Instead of setting the specified header, it adds a value.

Try specifying contentType and check what is returned from the server:

 $.ajax({ url: 'js/jquery.desobbcode.json', dataType: 'json', contentType: "application/json; charset=utf-8", ... }); 

You can also try sending empty data:

 $.ajax({ url: 'js/jquery.desobbcode.json', dataType: 'json', contentType: "application/json; charset=utf-8", data: {} ... }); 
+5
source

If you use php script to echo your json as strings, just put

 header('Content-Type: application/json; charset=utf-8'); 

before

 echo $jsonString 

line.

+2
source

These are new lines in JSON. This should be parsed in IE:

 {"inputButton":[{"id":"desoBBCode_bold","value":"Gras","tag":"b"},{"id":"desoBBCode_italic","value":"Italique","tag":"i"},{"id":"desoBBCode_underline","value":"Souligné","tag":"u"},{"id":"desoBBCode_image","value":"Image","tag":"img"},{"id":"desoBBCode_link","value":"Lien","tag":"url"},{"id":"desoBBCode_quote","value":"Citation","tag":"quote"}],"selectTextSize":[{"text":"Taille","value":""},{"text":"Trèstrèspetit","value":"1"},{"text":"Trèspetit","value":"2"},{"text":"Petit","value":"3"},{"text":"Gros","value":"4"},{"text":"Trèsgros","value":"5"},{"text":"Trèstrèsgros","value":"6"}],"selectTextColor":[{"text":"Couleur","value":"a"},{"text":"Rouge","value":"red"},{"text":"Bleu","value":"blue"},{"text":"Vert","value":"green"}]} 

Real-time example: http://jsbin.com/umahiq/edit

+1
source

Perhaps try setting contentType to "text / json" instead of "application / json". In the past, I had some problems using "application / json", and using "text / json" always seemed to work better.

0
source

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


All Articles