Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I've got two pages that use the same js-file to call certain PHP-file and get data from there in JSON format. Although the data that gets in the PHP-file AND data that gets out is exactly the same, Ajax on the second page returns 'parsererror' SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

        $.ajax({
        type: 'POST',
        dataType: "json",
        data: {objtyp: this.objtyp, objid: this.objid},
        url: '/admin/getfieldsadd.php', 
        success: function(data) {
        //not going to happen
        },

        error: function (xhr, status, text) {
          switch (status) {
             case 404:
                 alert('File not found');
                 break;
             case 500:
                 alert('Server error');
                 break;
             case 0:
                 alert('Request aborted');
                 break;
             default:
                 alert('Unknown error: ' + status + " " + text);
            }
        }

So have anybody encountered the same problem?

share|improve this question
1  
Post your JSON. – void Feb 15 at 7:26
    
Look at the response in Developer Tools. Make sure there isn't anything in the response other than the JSON. – Barmar Feb 15 at 7:41
    
@Barmar, here – Moorindal Feb 15 at 12:23
    
@void, previous comment. – Moorindal Feb 15 at 13:53

3 Answers 3

This sounds reminiscent of the dreaded BOM. Excerpt from that link:

At the beginning of a page that uses a Unicode character encoding you may find some bytes that represent the Unicode code point U+FEFF BYTE ORDER MARK (abbreviated as BOM).

The BOM, when correctly used, is invisible.

Perhaps check that the file's encoding is set to UTF8 Without BOM.

share|improve this answer
    
While this is possible, the more common reason is printing something in the script before calling echo json_encode(). – Barmar Feb 15 at 7:40
    
@Barmar, what do you mean? PHP or JS file? – Moorindal Feb 15 at 12:26

Maybe a mime type error.

Try to add the beforeSend property like this in your AJAX call :

$.ajax({
    type: 'POST',
    dataType: "json",
    data: {objtyp: this.objtyp, objid: this.objid},
    url: '/admin/getfieldsadd.php',
    beforeSend: function(x) {
        if(x && x.overrideMimeType) {
            x.overrideMimeType("application/json");
        }
    },
    ...
}
share|improve this answer
    
It didn't help, alas. – Moorindal Feb 17 at 4:29
up vote 0 down vote accepted

It appears the trouble was in jQuery version. Now that it's updated all seems to work fine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.