[英]Issues with jQuery, ajax, and jsonp
I am using jsonp and ajax to access a web service on another server. 我正在使用jsonp和ajax访问另一台服务器上的Web服务。 Here's the jQuery: 这是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: 这是我正在调用的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": ""
}
I tried using the getJSON jQuery method first, with the same result. 我首先尝试使用getJSON jQuery方法,结果相同。 Thought I'd try the base ajax method, since it has a bit more control, but as you can see, no luck. 以为我会尝试基础ajax方法,因为它有更多的控制权,但正如你所看到的,没有运气。 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? 知道为什么它会抛出一个错误并给我一个statusText属性的成功值吗?
Alright, I added some options to the ajax call, and removed the callback param from the url. 好吧,我在ajax调用中添加了一些选项,并从url中删除了回调参数。 Here's the new ajax call: 这是新的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);
}
});
I'm getting a new error, which is good I suppose, but still not working. 我收到了一个新的错误,我想这很好,但仍然没有用。 Difference is that the textStatus is now "parsererror". 区别在于textStatus现在是“parsererror”。 The console is also throwing a syntax error on line one of the json file: 控制台也在json文件的第一行抛出语法错误:
Uncaught SyntaxError: Unexpected token :
Ideas? 想法?
Okay a couple of things jump out at me: 好吧有几件事情向我跳出来:
$.ajax({
type: 'GET',
#You do not need to append the callback as you have jsonp configured it will do it
#automatically append the callback=<auto generated name>
url: wsurl,
dataType: 'jsonp',
crossDomain: true,
error: function(data) {
console.log('error', data);
},
success: function(data) {
console.log('success', data);
},
complete: function() {
console.log('done');
}
});
Also your return does not to appear to be wrapped in a function which is required for jsonp to work. 此外,您的返回似乎不会包含在jsonp工作所需的函数中。
<auto generated name>({ json object })
The callback function will be named by jquery automatically. 回调函数将由jquery自动命名。 So you need a service that will take in a callback parameter and return a json object with padding. 所以你需要一个服务来接受一个回调参数并返回一个带填充的json对象。
Not sure if this is the problem, but your url is not correct. 不确定这是否是问题,但您的网址不正确。 According to the jQuery docs, it should automatically append the ?callback=? 根据jQuery文档,它应该自动附加?callback =? for you: 为了你:
http://api.jquery.com/jQuery.ajax/ http://api.jquery.com/jQuery.ajax/
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): 从URL中删除回调并尝试用这样的东西包装你的json(php示例,$ _GET ['callback']由jQuery自动设置):
$_GET['callback'] . '({
"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": ""
})'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.