最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught SyntaxError: Unexpected token { - Stack Overflow

programmeradmin1浏览0评论

Error prompted when execute console.log($obj.longurl) from the Chrome Developer Console

Uncaught SyntaxError: Unexpected token { 
$.ajaxplete 
L jquery.min.js:19
N

Below is the script I execute from a HTML page and submit a form to call an external PHP file.

Javascript is called from .3.2/jquery.min.js

$('#shortener').submit(function(e) {
    e.preventDefault();
    $('#status').text('');
    $.ajax({
        cache: false,
        type: "POST",
        dataType: "json",
        data: $('#shortener').serialize(),
        url: $('#shortener').attr('action'),
        plete: function (XMLHttpRequest, textStatus) {
            console.log(XMLHttpRequest);
            $obj = JSON.parse(XMLHttpRequest.response);
            if ($obj.loginResult == "Passed") {
                ($('#longurl').val() === "") ? console.log("Empty longurl") : console.log($obj.longurl);
            } else {
                $('#status').text("Login Failed");
            };
        }
    });
    return false;
});

PHP

echo json_encode(array('loginResult' =>'Passed'));
echo json_encode(array('longurl' => BASE_HREF . $shortened_url));

typeof $obj.longurl is string but don't know why it can be returned to the $('#shortener').val(), anyone have similar experience and have the solution?

Error prompted when execute console.log($obj.longurl) from the Chrome Developer Console

Uncaught SyntaxError: Unexpected token { 
$.ajax.plete 
L jquery.min.js:19
N

Below is the script I execute from a HTML page and submit a form to call an external PHP file.

Javascript is called from http://ajax.googleapis./ajax/libs/jquery/1.3.2/jquery.min.js

$('#shortener').submit(function(e) {
    e.preventDefault();
    $('#status').text('');
    $.ajax({
        cache: false,
        type: "POST",
        dataType: "json",
        data: $('#shortener').serialize(),
        url: $('#shortener').attr('action'),
        plete: function (XMLHttpRequest, textStatus) {
            console.log(XMLHttpRequest);
            $obj = JSON.parse(XMLHttpRequest.response);
            if ($obj.loginResult == "Passed") {
                ($('#longurl').val() === "") ? console.log("Empty longurl") : console.log($obj.longurl);
            } else {
                $('#status').text("Login Failed");
            };
        }
    });
    return false;
});

PHP

echo json_encode(array('loginResult' =>'Passed'));
echo json_encode(array('longurl' => BASE_HREF . $shortened_url));

typeof $obj.longurl is string but don't know why it can be returned to the $('#shortener').val(), anyone have similar experience and have the solution?

Share Improve this question edited Dec 18, 2013 at 9:00 웃웃웃웃웃 12k15 gold badges62 silver badges94 bronze badges asked Dec 18, 2013 at 8:31 KelvinKelvin 5851 gold badge7 silver badges27 bronze badges 3
  • As a side note, try to indent your code properly next time, the way it is is a bit hard to read, especially when the major problem is with the block syntax, which will be easier to solve if the code is properly indented. – casraf Commented Dec 18, 2013 at 8:40
  • Have you checked that your response is valid JSON? – CodingIntrigue Commented Dec 18, 2013 at 8:42
  • merge 2 arrays and put one echo json_encode – Robert Commented Dec 18, 2013 at 8:51
Add a ment  | 

1 Answer 1

Reset to default 6

Your PHP code is producing invalid JSON. You are basically echoing two JSON encoded objects after each other, which overall results in invalid JSON. It will look like:

{"loginResult": "Passed"} {"longurl": "<some URL>"}

The second { is the syntax error.

It should either be an array of objects (although that would be a strange structure)

[{"loginResult": "Passed"}, {"longurl": "<some URL>"}]

or one object

{"loginResult": "Passed", "longurl": "<some URL>"}

Create and encode one array:

echo json_encode(array(
    'loginResult' => 'Passed',
    'longurl' => BASE_HREF . $shortened_url
));

Another problem might be that, at least officially, the jqXHR object passed to the plete callback doesn't have a .response property. Since you also already set the dataType: 'json' option, there is no need for you to parse the response explicitly.

Here is an improved version of your code:

$.ajax({
    cache: false,
    type: "POST",
    dataType: "json",
    data: $('#shortener').serialize(),
    url: $('#shortener').attr('action'),
}).done(function (data) {
    if (data.loginResult == "Passed") {
        ($('#longurl').val() === "") ? console.log("Empty longurl") : console.log(data.longurl);
    } else {
        $('#status').text("Login Failed");
    }
});
发布评论

评论列表(0)

  1. 暂无评论