【发布时间】:2013-03-29 14:41:04
【问题描述】:
我正在尝试从我的朋友目前正在处理的这个 API 中输出一个变量。 API 是完全开放的,因此不需要身份验证。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://profusum.se/js/jsonp.js"></script>
</head>
<body>
<script>
$.jsonp({
url: "http://blackcab.didair.se/api/drivers",
//callbackParameter: '',
timeout: 45000,
success: function (driversData, statusTwo) {
$.each(driversData, function (i, data) {
var name = driversData.drivers[1].name;
document.write("<p> Namn: " + name);
});
},
});
</script>
</body>
</html>
但在使用 Chrome 开发者工具时出现此错误:
Uncaught SyntaxError: Unexpected token : drivers:1
为什么会出现这个错误?
【问题讨论】:
-
blackcab.didair.se/api/drivers&callback=hi 没有返回 jsonp。正确的 jsonp 是 hi(the_object) 而不是 {the_object}
-
可以在此处找到有效的 JSONP 响应:googleapis.com/customsearch/v1?q=cars&callback=hi
-
此外,除了返回 json 而不是 javascript,它返回
application/json作为 content-type 而不是application/javascript,因为它应该是 jsonp。 -
好叫 Joachim,一直忘记响应头。
标签: javascript jquery ajax api jsonp