Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery, ajax request doesn't success with JSON on IE

I made an AJAX call and it works on FF & Chrome but not on IE 7-8-9. I'm 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 by adding contentType: 'application/json' but I receive the same output which is :

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

I checked my JSON file with JSONLint and it's OK. I checked if there is an extra comma and the content is also trimmed. See my JSON file

If I put dataType: 'text', I receive the OK alert but a debug popup too.

Could you help me? Regards.

like image 830
Syl Avatar asked Feb 23 '23 03:02

Syl


2 Answers

IE is known to have issues with implied content types.

... the new XmlHttpRequest class in Internet Explorer 7 doesn’t implement setRequestHeader very intuitively. Instead of setting the specified header, it appends the value.

Try specifying a contentType and check what's coming back from the server:

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

You may also want to try sending blank data:

$.ajax({
    url: 'js/jquery.desobbcode.json',
    dataType: 'json',
    contentType: "application/json; charset=utf-8",
    data: {}
    ...
});
like image 137
Terry Avatar answered Mar 05 '23 03:03

Terry


If you are using php script to echo your json as a string just put

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

before

echo $jsonString

line.

like image 45
Dimitar Slavchev Avatar answered Mar 05 '23 02:03

Dimitar Slavchev