跨域请求中的json不正确

时间:2014-04-13 12:56:32

标签: javascript json rest

这是我的代码

$.getJSON('http://../id=1397391950253&callback=?', null, function (results) {
            alert('Cross domain JS call achieved');
});

结果是 -

{
    "data": [{
        "shiftY": 0,
        "regType": "FAS",
        "fuelVolume": 2373,
        "eventDate": 166625019,
        "gsmOn": true,
        "bigIconHeight": 21,
        "speed": 17,
        "roamingOn": false,
        "direction": 134,            
        "latitude": 54.8658849,
        "bigShiftY": 0,
        "iconHeight": 28,
        "useDiscreteOutput": false
    }],
    "reportDescriptor": {
        "reportTypeID": 2,
        "objectTypes": ",FAS,FTC",
        "dependencyType": "vehicle"
    }
}

但chrome告诉我这个错误" Uncaught SyntaxError:意外的令牌:rtmap:1" 结果出了什么问题?

1 个答案:

答案 0 :(得分:0)

跨域请求可以通过两种方式JSONPCORS完成。

我希望您使用JSONP,这意味着返回结果应该是名为callback的函数的参数(可以通过更改请求中的查询参数callback来更改函数名称例如:&callback=myCustomCallback)。您对服务器的响应应该是

callback({
    "data": [{
        "shiftY": 0,
        "regType": "FAS",
        "fuelVolume": 2373,
        "eventDate": 166625019,
        "gsmOn": true,
        "bigIconHeight": 21,
        "speed": 17,
        "roamingOn": false,
        "direction": 134,            
        "latitude": 54.8658849,
        "bigShiftY": 0,
        "iconHeight": 28,
        "useDiscreteOutput": false
    }],
    "reportDescriptor": {
        "reportTypeID": 2,
        "objectTypes": ",FAS,FTC",
        "dependencyType": "vehicle"
    }
})