我的代码是:

function getCategoryResponse() {
    var appid = "1";
    $.ajax({
        type: 'GET',
        url: 'http://itunes.apple.com/WebObjects/MZStoreServices.woa/ws/genres?id=36&callback=myCallbackFunction',
        data: {},
        dataType: 'jsonp',
        success: function (data) {
            var appData = data.results[0]; //get first record from array
            console.log(appData);
            return;
        },
        error: function () {
            //alert('something bad happened');
            console.log("something bad happened");
        }
    });
}


结果是:

"Uncaught SyntaxError: Unexpected token :" /* on chrome  */

"SyntaxError: missing ; before statement" /* on firefox */

最佳答案

该URL不会返回JSONP响应,而只会返回像这样开始的JSON响应:

{
    "36": {
        "name": "App Store",
        "id": "36",
        "url": "https://itunes.apple.com/us/genre/ios/id36?mt=8",
        "rssUrls": {
            "topAppsByRevenue": "https://itunes.apple.com/us/rss/topappsbyrevenue/genre=36/json",
            "topPaidIpadApplications": "https://itunes.apple.com/us/rss/toppaidipadapplications/genre=36/json",
            "topIpadAppsByRevenue": "https://itunes.apple.com/us/rss/topipadappsbyrevenue/genre=36/json",
            "topFreeIpadApplications": "https://itunes.apple.com/us/rss/topfreeipadapplications/genre=36/json",


您必须查阅文档以了解如何告诉您您需要JSONP,它的开始如下:

myCallbackFunction({
    "36": {
        "name": "App Store",
        "id": "36",
        "url": "https://itunes.apple.com/us/genre/ios/id36?mt=8",
        "rssUrls": {
            "topAppsByRevenue": "https://itunes.apple.com/us/rss/topappsbyrevenue/genre=36/json",
            "topPaidIpadApplications": "https://itunes.apple.com/us/rss/toppaidipadapplications/genre=36/json",
            "topIpadAppsByRevenue": "https://itunes.apple.com/us/rss/topipadappsbyrevenue/genre=36/json",
            "topFreeIpadApplications": "https://itunes.apple.com/us/rss/topfreeipadapplications/genre=36/json",


另外:使用jQuery时,最好允许jQuery指定回调函数的名称,而不是自己指定。

关于javascript - 未捕获到的SyntaxError:意外的 token :在chrome上,“SyntaxError:丢失; Firefox上的“声明前”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23263991/

10-13 02:39