无法签入 ajax
Cannot put checking in ajax
我有一个带按钮的引导框对话框。那里的第一个 ajax 函数有问题。
我想检查从模型和控制器返回的值是 0 还是 1,如果 returns 的值是 1,则停止回调函数将数据发布到数据库。但是,一旦我进行了检查,它就不会在检查第一个 ajax 函数之前无法正常工作,一切正常。现在,我什至无法将任何数据保存到数据库中。
buttons: {
success: {
label: "Save",
className: "btn-success",
callback: function() {
console.log('save');
console.log($('#re_confirm')[0].checked);
...
...
...
var check;
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/check_occupied",
type: "post", // To protect sensitive data
data: {
"table_id" : valueid2,
"session_id" : table_column_14,
//and any other variables you want to pass via POST
},
success:function(response){
// Handle the response object
check=response;
},
});
if(check!=0)
return;
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/update_booking",
type: "post",
data: {
"table_id" : $('#idtable').val(),
},
success: function(response){
...
}
});
}
},
...
,
...
}
});
$.ajax 是异步的
不过您的代码很容易修复:
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/check_occupied",
type: "post", // To protect sensitive data
data: {
"table_id": valueid2,
"session_id": table_column_14,
//and any other variables you want to pass via POST
},
success: function(response) {
// Handle the response object
if (response == 0) {
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/update_booking",
type: "post",
data: {
"table_id": $('#idtable').val(),
},
success: function(response) {
...
}
});
}
},
});
我有一个带按钮的引导框对话框。那里的第一个 ajax 函数有问题。 我想检查从模型和控制器返回的值是 0 还是 1,如果 returns 的值是 1,则停止回调函数将数据发布到数据库。但是,一旦我进行了检查,它就不会在检查第一个 ajax 函数之前无法正常工作,一切正常。现在,我什至无法将任何数据保存到数据库中。
buttons: {
success: {
label: "Save",
className: "btn-success",
callback: function() {
console.log('save');
console.log($('#re_confirm')[0].checked);
...
...
...
var check;
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/check_occupied",
type: "post", // To protect sensitive data
data: {
"table_id" : valueid2,
"session_id" : table_column_14,
//and any other variables you want to pass via POST
},
success:function(response){
// Handle the response object
check=response;
},
});
if(check!=0)
return;
$.ajax({
url : "<?php echo base_url(); ?>index.php/home/update_booking",
type: "post",
data: {
"table_id" : $('#idtable').val(),
},
success: function(response){
...
}
});
}
},
...
,
...
}
});
$.ajax 是异步的
不过您的代码很容易修复:
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/check_occupied",
type: "post", // To protect sensitive data
data: {
"table_id": valueid2,
"session_id": table_column_14,
//and any other variables you want to pass via POST
},
success: function(response) {
// Handle the response object
if (response == 0) {
$.ajax({
url: "<?php echo base_url(); ?>index.php/home/update_booking",
type: "post",
data: {
"table_id": $('#idtable').val(),
},
success: function(response) {
...
}
});
}
},
});