gpt4 book ai didi

javascript - jQuery、ajax 和 jsonp 的问题

转载 作者:搜寻专家 更新时间:2023-11-01 04:38:35 25 4
gpt4 key购买 nike

我正在使用 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 :

想法?

最佳答案

好吧,我突然想到了一些事情:

$.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');
}
});

此外,您的返回似乎没有包含在 jsonp 工作所需的函数中。

<auto generated name>({ json object })

回调函数将由 jquery 自动命名。因此,您需要一个服务,该服务将接受一个回调参数并返回一个带有填充的 json 对象。

关于javascript - jQuery、ajax 和 jsonp 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087034/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com