【问题标题】:Incorrect json in cross domain resquest跨域请求中的json不正确
【发布时间】:2014-04-13 12:56:32
【问题描述】:

这是我的代码

$.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: Unexpected token : rtmap:1” 结果有什么问题?

【问题讨论】:

  • @SLaks,如何解决这个问题,我必须向外部资源提出请求?
  • 如果外部资源没有为您提供访问它的方法(JSONP 或 CORS),您就不能这样做。 (除非你做一个服务器端代理)

标签: javascript json rest


【解决方案1】:

跨域请求可以通过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"
    }
})

【讨论】:

  • 不幸的是,服务器没有响应 JSONP 规范:(
猜你喜欢
  • 2011-01-04
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2012-10-31
  • 2012-12-19
  • 2023-04-03
相关资源
最近更新 更多