jQuery、ajax 和 jsonp 的问题
我正在使用 jsonp 和 ajax 访问另一台服务器上的 Web 服务。这是 jQuery:
$.ajax({
type: 'GET',
url: wsurl + 'callback=?',
dataType: 'jsonp',
crossDomain: true,
error: function(data) {
console.log('error', data);
},
success: function(data) {
console.log('success', data);
},
complete: function() {
console.log('done');
}
});
问题是正在调用错误回调。它给了我这个非常有用的信息:
{
readyState: 4,
status: 200,
statusText: "success"
}
这是我正在调用的 json 文件:
{
"id": 0,
"room_number": "0",
"first_name": "Admin",
"last_name": "Istrator",
"password": "",
"salutation": "Mr.",
"telephone": "",
"email": "",
"description": "admin",
"checkin_date": 915797106000,
"checkout_date": 4071557106000,
"last_login_date": 947333106000,
"active_status": true,
"created_date": 915797106000,
"created_by": 0,
"reference_id": ""
}
我首先尝试使用 getJSON jQuery 方法,得到了相同的结果。我想我会尝试基本的 ajax 方法,因为它有更多的控制权,但正如你所看到的,没有运气。那么,注意到我做错了什么吗?知道为什么它会抛出错误并为我提供 statusText 属性的成功值吗?
编辑
好吧,我向 ajax 调用添加了一些选项,并从 url 中删除了回调参数。这是新的 ajax 调用:
$.ajax({
type: 'GET',
url: wsurl,
dataType: 'jsonp',
crossDomain: true,
error: function(xhr, textStatus, errorThrown) {
console.log('textStatus: ' + textStatus);
},
success: function(data) {
console.log('success');
console.log(data);
}
});
我收到一个新错误,我认为这很好,但仍然无法正常工作。不同之处在于 textStatus 现在是“parsererror”。控制台还在 json 文件的第一行抛出语法错误:
Uncaught SyntaxError: Unexpected token :
想法?
I am using jsonp and ajax to access a web service on another server. Here's the jQuery:
$.ajax({
type: 'GET',
url: wsurl + 'callback=?',
dataType: 'jsonp',
crossDomain: true,
error: function(data) {
console.log('error', data);
},
success: function(data) {
console.log('success', data);
},
complete: function() {
console.log('done');
}
});
The issue is that the error callback is being called. It gives me this wonderfully helpful information:
{
readyState: 4,
status: 200,
statusText: "success"
}
And here is the json file I am calling:
{
"id": 0,
"room_number": "0",
"first_name": "Admin",
"last_name": "Istrator",
"password": "",
"salutation": "Mr.",
"telephone": "",
"email": "",
"description": "admin",
"checkin_date": 915797106000,
"checkout_date": 4071557106000,
"last_login_date": 947333106000,
"active_status": true,
"created_date": 915797106000,
"created_by": 0,
"reference_id": ""
}
I tried using the getJSON jQuery method first, with the same result. Thought I'd try the base ajax method, since it has a bit more control, but as you can see, no luck. So, notice anything I'm doing wrong? Have any idea why it is throwing an error and giving me a successful value for the statusText property?
EDIT
Alright, I added some options to the ajax call, and removed the callback param from the url. Here's the new ajax call:
$.ajax({
type: 'GET',
url: wsurl,
dataType: 'jsonp',
crossDomain: true,
error: function(xhr, textStatus, errorThrown) {
console.log('textStatus: ' + textStatus);
},
success: function(data) {
console.log('success');
console.log(data);
}
});
I'm getting a new error, which is good I suppose, but still not working. Difference is that the textStatus is now "parsererror". The console is also throwing a syntax error on line one of the json file:
Uncaught SyntaxError: Unexpected token :
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,有几件事引起了我的注意:
此外,您的返回似乎没有包含在 jsonp 工作所需的函数中。
回调函数将由 jquery 自动命名。因此,您需要一个接受回调参数并返回带填充的 json 对象的服务。
Okay a couple of things jump out at me:
Also your return does not to appear to be wrapped in a function which is required for jsonp to work.
The callback function will be named by jquery automatically. So you need a service that will take in a callback parameter and return a json object with padding.
不确定这是否是问题所在,但您的网址不正确。根据 jQuery 文档,它应该自动附加 ?callback=?适合您:
http://api.jquery.com/jQuery.ajax/
Not sure if this is the problem, but your url is not correct. According to the jQuery docs, it should automatically append the ?callback=? for you:
http://api.jquery.com/jQuery.ajax/
从 URL 中删除回调并尝试用类似这样的内容包装你的 json(例如,$_GET['callback'] 由 jQuery 自动设置):
Remove the callback from the URL and try and wrap your json with something like this (php example, $_GET['callback'] is automatically set by jQuery):