gpt4 book ai didi

ajax - 简单的jQuery/Ajax错误

转载 作者:行者123 更新时间:2023-12-03 08:34:09 25 4
gpt4 key购买 nike

我有以下代码:

var custID = 1;

$.ajax({
url: 'php/viewCustomer.php',
type: 'GET',
data: '{custID: ' + custID + '}',
dataType: 'json',
cache: false,
beforeSend: function () {
$('#display').append('<div id="loader"> Lodaing ... </div>');
},
complete: function () {
$('#loader').remove();
},
success: function (data) {
//do something
},
error: function () {
alert('could not process');
}
});

出现错误并警告错误消息 could not process,因此我尝试像这样调试它:
var custID = 1;

$.ajax({
url: 'php/viewCustomer.php',
type: 'GET',
data: '{custID: ' + custID + '}',
dataType: 'json',
cache: false,
beforeSend: function () {
$('#display').append('<div id="loader"> Lodaing ... </div>');
},
complete: function () {
$('#loader').remove();
},
success: function (data) {
//do something
},
error: function (jqXHR) {
alert('Error: ' + jqXHR.status + jqXHR.statusText);
}
});

输出:
200 OK

因此,如果可以,为什么它会执行error:函数呢?感到困惑,请帮忙。

最佳答案

如果您打算将其作为JSON对象,则数据字符串格式错误。之前对此有一个疑问:Jquery passing data to ajax function

相反,请尝试:

data: JSON.stringify({custID: custID}),

格式为(key):( variable)。我以前的回答是在变量周围加上引号,这不是必需的。

关于ajax - 简单的jQuery/Ajax错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15254083/

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