Sunday, April 6, 2014

[jQuery][Json][Solved] Uncaught SyntaxError: Unexpected token o

The json sample:
({
    "status": "ok",
    "type": "img",
    "items": [
        {
            "title": "\u65b9\u6cd5 ",
            "img": "http:\/\/www.abc.com.hk\/086347434_n.jpg",
            "url": "http:\/\/www.abc.com.hk\/DK.php?id=ADkRZBEr"
        },
        {
            "title": "\u6703\u54e1\u535a\u5ba2",
            "img": "http:\/\/www.abc.com.hk\/993.jpg",
            "url": "http:\/\/www.abc.com.hk\/ADsRZBEuA3QMKQ\/"
        }
    ]
})

 my case is using jquery JSON.parse to parse json :

    var url;
    url = "http://www.abc.com.hk/ul?id="+id+"&jsoncallback=?";
    $.getJSON(url, function (json) {
        if (json.status == "ok") {
            result[json.type] = json.status;
            if (currentType == json.type) { swapItemsTo(json.type, true);}
        } else {
        }
        var json = JSON.parse(json);
        console.log(json.type);

    }).error(function(json){
    });
By found the json in my case is already in json format,
so don't need to parse the json,
you can call the json value directly is okay,
It's won't cause the SyntaxError:

    var url;
    url = "http://www.abc.com.hk/ul?id="+id+"&jsoncallback=?";
    $.getJSON(url, function (json) {
        if (json.status == "ok") {
            result[json.type] = json.status;
            if (currentType == json.type) { swapItemsTo(json.type, true);}
        } else {
            alert(json.message);
        }
        console.log(json.type);
    }).error(function(json){
    });

So if you see the error "Uncaught SyntaxError: Unexpected token o ", please check is the value you call is already in json format.

Reference:
http://stackoverflow.com/questions/8081701/i-keep-getting-uncaught-syntaxerror-unexpected-token-o

No comments :

Post a Comment