Javascript 未调用第二个成功函数中的Ajax函数
我正在使用Javascript 未调用第二个成功函数中的Ajax函数,javascript,jquery,ajax,datatables,Javascript,Jquery,Ajax,Datatables,我正在使用DataTables库,在这里我需要连续发出AJAX请求。 问题是没有调用第二个Success函数中的AJAX函数。 这是全部代码 $(document).ready(()=>{ $.ajax({ url: 'http://localhost:3000/api/post/getUserData', method: 'post', dataType: 'json', data: { &q
DataTables
库,在这里我需要连续发出AJAX
请求。
问题是没有调用第二个Success
函数中的AJAX
函数。
这是全部代码
$(document).ready(()=>{
$.ajax({
url: 'http://localhost:3000/api/post/getUserData',
method: 'post',
dataType: 'json',
data: {
"email": window.email,
"token": window.token
},
success: function(data){ // <-----------------------FIRST SUCCESS FUNCTION
let table = $('#datatable').dataTable({
data: data,
"autoWidth": true,
columns: [
{'data': 'id'},
{'data': 'main'},
{'data': 'shrinked'},
{'data': 'clicks'},
{"data": "id",
"render": function ( data, type, row, meta ) {
return `<button data-id=${data} onclick="disableThis(event)" class="btn btn-warning">Disable</button>`
}
},
{"data": "id",
"render": function ( data, type, row, meta ) {
return `<button data-id=${data} onclick="deleteThis(event)" class="btn btn-danger">Delete</button>`
}
}
]
})
//------------------------------Function to add new Row inside Datatable when Form is submmitted
$(function () {
$('#form').bind('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'http://localhost:3000/userShrink',
type: 'post',
datatype: 'json',
data: {
fullUrl: $('#Url').val(),
email: window.email,
token: window.token
},
success: function () { // <-----------------SECOND SUCCESS FUNCTION (THIS ONE IS NOT EXECUTING)
$.ajax({
url: 'http://localhost:3000/api/post/getUserData',
method: 'post',
dataType: 'json',
data: {
"email": window.email,
"token": window.token
},
success: function(data2){
console.log(data2)
}
})
}
});
});
});
}
})
})
在此之后,如果表单
被提交,它将触发此事件,然后将
中的数据成功发布到数据库中
//------------------------------Function to add new Row inside Datatable when Form is submmitted
$(function () {
$('#form').bind('submit', function (event) {
event.preventDefault();
$.ajax({
url: 'http://localhost:3000/userShrink',
type: 'post',
datatype: 'json',
data: {
fullUrl: $('#Url').val(),
email: window.email,
token: window.token
},
当第二个success
函数被执行或没有执行时,情况会出错,我需要第二个success函数的原因是将新提交的数据加载到数据表中
success: function () { // <-----------------SECOND SUCCESS FUNCTION (THIS ONE IS NOT EXECUTING)
$.ajax({
url: 'http://localhost:3000/api/post/getUserData',
method: 'post',
dataType: 'json',
data: {
"email": window.email,
"token": window.token
},
success: function(data2){
console.log(data2)
}
})
}
success:function(){/自己修复了它,
这是立即添加行
功能
$(function () {
$('#form').bind('submit', async function (event) {
// using this page stop being refreshing
event.preventDefault();
let data = {
'fullUrl': $('#Url').val(),
'email': window.email,
'token': window.token
}
let data2 = {
'email': window.email,
'token': window.token
}
$.post('http://localhost:3000/userShrink', data);
setTimeout(()=>{
$.post('http://localhost:3000/api/post/getUserData', data2).done(function (res){
$('#datatable').DataTable().row.add(res[res.length-1]).draw();
});
}, 1000)
})
});
您是否厌倦了在第一个ajax调用之外移出add row函数?希望这有帮助。是的,我有,但仍然不起作用。从ajax调用中移出第二个success函数back@wiseone我在发帖之前已经试过了,也没用。
$(function () {
$('#form').bind('submit', async function (event) {
// using this page stop being refreshing
event.preventDefault();
let data = {
'fullUrl': $('#Url').val(),
'email': window.email,
'token': window.token
}
let data2 = {
'email': window.email,
'token': window.token
}
$.post('http://localhost:3000/userShrink', data);
setTimeout(()=>{
$.post('http://localhost:3000/api/post/getUserData', data2).done(function (res){
$('#datatable').DataTable().row.add(res[res.length-1]).draw();
});
}, 1000)
})
});