admin管理员组

文章数量:1278719

I have this code:

    $.ajax({
            dataType: 'text',
            url: '/_/js/answers.json',
            type: "GET",
            success: function (data) {
                alert(data);
                alert(data.code);
                var result = JSON.parse(data);
                var hey = JSON.parse('{"code": 123}'); 
                alert(hey.code);
                alert(result.code);
            },
            error: function () {
                alert("code not found");
            }
        });

In the first alert, alert(data) it shows me '{"code": 123}', in the second alert alert(data.code), it tells me undefined, in the third alert alert(hey.code), it shows me 123, and that's what I want, but in the fourth alert, the console tells me Uncaught SyntaxError: Unexpected token '.
When I change the JSON.parse to $.parseJSON, it does exactly the same things.
I don't know what's wrong, the json is fine (exactly the same as the json in var hey).

I passed the json to the server like this: javascript:

var json = {code: code};
        json = JSON.stringify(json);
        json = {data: json};

        $.ajax({
            url: "/_/js/write-json.php",
            type: "POST",
            dataType: 'json',
            data: json
        }); 

php:

    <?php
    $myFile = "answers.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh,var_export($_POST['data'], true));
    fclose($fh);
    ?>

Thanks, bhc11.

I have this code:

    $.ajax({
            dataType: 'text',
            url: '/_/js/answers.json',
            type: "GET",
            success: function (data) {
                alert(data);
                alert(data.code);
                var result = JSON.parse(data);
                var hey = JSON.parse('{"code": 123}'); 
                alert(hey.code);
                alert(result.code);
            },
            error: function () {
                alert("code not found");
            }
        });

In the first alert, alert(data) it shows me '{"code": 123}', in the second alert alert(data.code), it tells me undefined, in the third alert alert(hey.code), it shows me 123, and that's what I want, but in the fourth alert, the console tells me Uncaught SyntaxError: Unexpected token '.
When I change the JSON.parse to $.parseJSON, it does exactly the same things.
I don't know what's wrong, the json is fine (exactly the same as the json in var hey).

I passed the json to the server like this: javascript:

var json = {code: code};
        json = JSON.stringify(json);
        json = {data: json};

        $.ajax({
            url: "/_/js/write-json.php",
            type: "POST",
            dataType: 'json',
            data: json
        }); 

php:

    <?php
    $myFile = "answers.json";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh,var_export($_POST['data'], true));
    fclose($fh);
    ?>

Thanks, bhc11.

Share Improve this question edited Apr 19, 2014 at 17:32 bhc11 asked Apr 19, 2014 at 16:59 bhc11bhc11 1691 gold badge2 silver badges14 bronze badges 14
  • It might be that your JSON is invalid, to be sure check it here – bobthedeveloper Commented Apr 19, 2014 at 17:00
  • 3 you say that your alert data states '{"code": 123}'. This includes single quotes which are not meant to be there. That is your problem. – Lee Taylor Commented Apr 19, 2014 at 17:04
  • 1 Single quotes are part of Javascript literal syntax, they aren't part of the JSON itself. – Barmar Commented Apr 19, 2014 at 17:06
  • 1 You can use JSON.stringify(data) or eval(data) before using it. – prava Commented Apr 19, 2014 at 17:10
  • 1 I think you should fix the server-side code to return valid JSON instead of taking a substring of the result when yur parse it. Just out of interest, any reason why your are not using dataType: 'json' and just getting an object as the data param? – Adam Commented Apr 19, 2014 at 17:15
 |  Show 9 more ments

2 Answers 2

Reset to default 5

The ' characters around your JSON make it a JavaScript string and don't form part of the data.

It looks like you have those characters in the JSON that you are requesting over HTTP so there they do form part of the data.

This is not valid JSON. Remove the quotes.

You should have:

{"code": 123}

Not

'{"code": 123}'

Try changing dataType to JSON:

$.ajax({
    dataType: 'JSON',
    url: '/_/js/answers.json',
    type: "GET",
    success: function (data) {
        alert(data);
        alert(data.code);
        var result = JSON.parse(data);
        var hey = JSON.parse('{"code": 123}'); 
        alert(hey.code);
        alert(result.code);
    },
    error: function () {
        alert("code not found");
    }
});

本文标签: javascriptAJAX json unexpected token 39Stack Overflow