Javascript Jquery发送Ajax,不等待发送其他Ajax的响应
我有一个ajax,它调用服务器并运行一个长过程(这个过程是在数据库上写入状态) 我有其他ajax(递归)来获取长进程的状态,并在进度条上设置参数 我的问题是,第二个ajax在第一个完成之前不会启动。有没有一种方法可以发送第一个ajax而不等待响应 有什么想法吗 我很感激任何建议,我对这个问题有点厌倦了 如果有其他方法发送长进程并获取长进程的状态,请告诉我 谢谢大家! 这是我的代码,以防万一Javascript Jquery发送Ajax,不等待发送其他Ajax的响应,javascript,jquery,ajax,progress-bar,no-response,Javascript,Jquery,Ajax,Progress Bar,No Response,我有一个ajax,它调用服务器并运行一个长过程(这个过程是在数据库上写入状态) 我有其他ajax(递归)来获取长进程的状态,并在进度条上设置参数 我的问题是,第二个ajax在第一个完成之前不会启动。有没有一种方法可以发送第一个ajax而不等待响应 有什么想法吗 我很感激任何建议,我对这个问题有点厌倦了 如果有其他方法发送长进程并获取长进程的状态,请告诉我 谢谢大家! 这是我的代码,以防万一 executeProgressBar(1,令牌); $.ajax({ 类型:“POST”, enctype
executeProgressBar(1,令牌);
$.ajax({
类型:“POST”,
enctype:“多部分/表单数据”,
processData:false,
contentType:false,
cache:false,
url:“/long\u进程”,
数据:表格数据,
成功:功能(响应){
//没什么
}
});
函数executeProgressBar(开始,标记){
如果(开始==1){
//重置进度条
$('.progress bar').css('width','0%');
$('.progress bar').text('0%');
$('.progress bar').attr('data-progress','0');
}
$.ajax({
类型:“POST”,
url:“/progress\u bar\u status”,
数据:{令牌:令牌,睡眠:0},
成功:功能(响应){
$('.progress bar').css('width',response['percentage']+'%');
$('.progress bar').text(响应['percentage']+'%');
$('.progress bar').attr('data-progress',response['percentage']);
$('完成').text(响应['执行]);
$('.execute time').text('tiempo');
如果(response.percentage==100){
$('.end process').show();
}否则{
executeProgressBar(0,令牌);
}
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
如果(textStatus=='parsererror'){
text状态='技术错误:服务器返回意外响应。发送已停止';
}
警报(文本状态);
}
});
}
编辑
我在服务器端用php解决了这个问题
/************** Close connection and return echo message **************/
ob_end_clean();
header("Connection: close");
ignore_user_abort(true);
ob_start();
echo('text response to ajax');
$size = ob_get_length();
header("Content-Length: $size");
ob_end_flush();
flush();
// if you're using sessions, this prevents subsequent requests
// from hanging while the background process executes
if (session_id()) {
session_write_close();
}
/************** background process starts here **************/
查看本文:您正在立即调用
executeProgressBar
,而不是等待第一个AJAX调用完成。在success
方法回调中调用executeProgressBar
函数,并将其传递给executeProgressBar
函数,然后修改参数,如下面的示例所示
确保构建一个签入,让递归函数知道何时停止
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
url: "/long_process",
data: form_data,
success: function (response) {
executeProgressBar(1, token, response);
}
});
function executeProgressBar(start, token, response) {
if (start == 1) {
//reset progress bar
$('.progress-bar').css('width', '0%');
$('.progress-bar').text('0%');
$('.progress-bar').attr('data-progress', '0');
}
$.ajax({
type: "POST",
url: "/progress_bar_status",
data: {token: token, sleep: 0},
success: function (response) {
$('.progress-bar').css('width', response['percentage'] + '%');
$('.progress-bar').text(response['percentage'] + '%');
$('.progress-bar').attr('data-progress', response['percentage']);
$('#done').text(response['executed']);
$('.execute-time').text('tiempo');
if (response.percentage == 100) {
$('.end-process').show();
} else {
executeProgressBar(0, token, response);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'parsererror') {
textStatus = 'Technical error: Unexpected response returned by server. Sending stopped.';
}
alert(textStatus);
}
});
}
您立即调用
executeProgressBar
,而不是等待第一个AJAX调用完成。在success
方法回调中调用executeProgressBar
函数,并将其传递给executeProgressBar
函数,然后修改参数,如下面的示例所示
确保构建一个签入,让递归函数知道何时停止
$.ajax({
type: "POST",
enctype: 'multipart/form-data',
processData: false,
contentType: false,
cache: false,
url: "/long_process",
data: form_data,
success: function (response) {
executeProgressBar(1, token, response);
}
});
function executeProgressBar(start, token, response) {
if (start == 1) {
//reset progress bar
$('.progress-bar').css('width', '0%');
$('.progress-bar').text('0%');
$('.progress-bar').attr('data-progress', '0');
}
$.ajax({
type: "POST",
url: "/progress_bar_status",
data: {token: token, sleep: 0},
success: function (response) {
$('.progress-bar').css('width', response['percentage'] + '%');
$('.progress-bar').text(response['percentage'] + '%');
$('.progress-bar').attr('data-progress', response['percentage']);
$('#done').text(response['executed']);
$('.execute-time').text('tiempo');
if (response.percentage == 100) {
$('.end-process').show();
} else {
executeProgressBar(0, token, response);
}
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
if (textStatus == 'parsererror') {
textStatus = 'Technical error: Unexpected response returned by server. Sending stopped.';
}
alert(textStatus);
}
});
}
嗨,谢谢你的回复。问题是第一个ajax需要10分钟。对于您的解决方案,当第一个ajax完成时,状态为100%(我的帖子中包含了额外的代码,第二个ajax现在应该包含在第一个ajax中)您好,感谢您的回复。问题是第一个ajax需要10分钟。对于您的解决方案,当第一个ajax完成时,状态为100%(我的文章中包含了额外的代码,第二个ajax现在应该在第一个ajax中)