【发布时间】:2014-11-24 03:51:21
【问题描述】:
在我解释我的问题之前,我想提一下我在jsonp 上是个幼稚的人。这实际上是我第一次尝试使用JSONP。
我正在使用 jquery ajax 调用从网站拉回数据。
我的 jquery 代码如下
$.fn.checkTPS = function(){
return this.each(function(){
var interval;
$(this).on('keyup', function() {
var api_key = 'asdfasfsadfsadfsad';
var format = 'json';
var username = 'dame@example.co.uk';
var self = $(this);
var selfValue;
var feedback = $('.tps-feedback');
if(interval === undefined){
interval = setInterval(function(){
if(selfValue !== self.val()) {
selfValue = self.val();
if (selfValue.length > 9){
$.ajax({
url: 'https://www.selectabase.co.uk/api/v1/tps/' + selfValue + '/',
type: 'get',
dataType: 'jsonp',
data: {
format: format,
username: username,
api_key: api_key
},
success: function(data) {
console.log(data);
},
error: function() {
},
jsonp: 'jsonp'
});
}
}
},3000);
}
});
});
};
我想提供来自selectabase.co.uk 的服务,据他们说这是我应该使用https://www.selectabase.co.uk/api/v1/tps/[number]/?format=json&username=[username]&api_key=[api key] 服务的方式
当我使用 ajax 发送请求时,我收到此错误 Uncaught SyntaxError: Unexpected token :,单击时会打开
{"ctps": false, "number": "1452500705", "resource_uri": "/api/v1/tps/01452500705/", "tps": false} 顺便说一下我想要但不知道这是什么错误 unexpected token :
我从检查元素选项卡复制了以下链接(您可以看到下图)我认为这是由 json https://www.selectabase.co.uk/api/v1/tps/01452500705/?jsonp=jQuery17102731868715648129_14120077325500&format=json&username=dame40example.co.uk&api_key=asdfasfsadfsadfsad&_=14120077325500 生成的调用
我从 chrome 中的 inspect element > source tab 复制了下面的链接。我想我应该添加一张图片来正确描述我从哪里复制了这个 json 数据和链接。
我希望我已经设法传达我的信息...如果您有任何想法我需要添加什么,请提供帮助...问候
【问题讨论】:
-
服务器正在发回 JSON,而不是 JSONP。这就是问题所在。
-
但是如果我使用
dataType: 'json'浏览器将请求发送到该 url -
@Sharif 服务器需要自己响应 JSONP。
标签: javascript jquery ajax json jsonp