【问题标题】:AngularJS issue parsing json using $HTTP.JSONP [duplicate]AngularJS 使用 $HTTP.JSONP 解析 json 问题 [重复]
【发布时间】:2015-08-27 01:23:03
【问题描述】:

我正在尝试使用 jsonp 方法从 angularjs 应用程序中的 wooCommerce api 获取数据

$http.jsonp('http://pwoo.dev/wc-api/v2?callback=JSON_CALLBACK')
.success(function ( data, status, headers, config ) {
    console.log(data, status, headers, config);
})
.error(function ( data, status, headers, config ) {
    console.log("Error", data, status, headers, config);
});

作为回报,它在控制台中给了我这个错误

Uncaught SyntaxError: Unexpected token :

并打破错误块中的代码

http://jsfiddle.net/zzlalani/5Lj46p69/

这是我在上面小提琴中使用的同一类型 api 的实时链接

https://sergedenimes.com/wc-api/v2

我已经在线验证了 json 及其格式的正确性和有效性,您可以在以下链接的 fiddle 控制台中查看它

https://jsfiddle.net/zzlalani/ru6kozxn/1/

更新:

好的,它不适用于jQuery.ajax,请检查下面的小提琴,其中我使用了简单 jquery 的$.ajax 方法和dataType: "jsonp"

https://jsfiddle.net/zzlalani/zvreh2xp/2/

我不明白问题出在哪里,因为在我上面的角度示例中,一个简单的带有数据的 test.json

{
    "name": "zeeshan Lalani"
}

抛出同样的错误。我在这里想念什么?这个世界上的人们如何使用JSONP 示例? :(

【问题讨论】:

  • 你的数据是 JSON,不是 JSONP。
  • @Quentin 我无法对 api 服务器进行更改,我应该对以jsonp 访问数据进行哪些更改,因为通过正常的get 调用我会遇到这些跨域问题。
  • 如果你想绕过阻止跨源请求的安全系统,你需要服务器的配合。如果您无法控制该服务器,请使用其他服务器。
  • @Quentin 不是有效的JSONP 吗? sergedenimes.com/wc-api/v2?_jsonp=JSON_CALLBACK
  • 这个网址显示GET http://pwoo.dev/wc-api/v2?_jsonp=angular.callbacks._0?callback=JSON_CALLBACK

标签: javascript json angularjs woocommerce jsonp


【解决方案1】:

jsonp 内容应该在回调函数之间,例如:

JSON_CALLBACK({"data":{"A":123,"B":true}})

【讨论】:

    猜你喜欢
    • 2012-08-17
    • 2013-07-28
    • 1970-01-01
    • 2015-06-02
    • 2012-11-12
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多