【发布时间】: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 :
并打破错误块中的代码
这是我在上面小提琴中使用的同一类型 api 的实时链接
我已经在线验证了 json 及其格式的正确性和有效性,您可以在以下链接的 fiddle 控制台中查看它
更新:
好的,它不适用于jQuery.ajax,请检查下面的小提琴,其中我使用了简单 jquery 的$.ajax 方法和dataType: "jsonp"
我不明白问题出在哪里,因为在我上面的角度示例中,一个简单的带有数据的 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