Php 2个连续的JQuery帖子,第二个帖子先执行
请帮我做这个 我必须将值传递给php文件进行保存,但是如果选中了复选框,它必须使用第一个文件的返回值执行第二个jquery post。但第二篇jquery文章似乎是先执行的,下面是我的代码:Php 2个连续的JQuery帖子,第二个帖子先执行,php,javascript,jquery,post,response,Php,Javascript,Jquery,Post,Response,请帮我做这个 我必须将值传递给php文件进行保存,但是如果选中了复选框,它必须使用第一个文件的返回值执行第二个jquery post。但第二篇jquery文章似乎是先执行的,下面是我的代码: if($action=="save_prod") { var remember = document.getElementById('chk'); var $suppid=document.getElementById('supp').value;
if($action=="save_prod")
{
var remember = document.getElementById('chk');
var $suppid=document.getElementById('supp').value;
var $categlist=document.getElementById('categlist').value;
var $prodname=document.getElementById('prodname').value;
var $proddesc=document.getElementById('proddesc').value;
$.post(baseurl+'index.php/main/newproduct',{"categlist" : $categlist,"prodname" : $prodname," proddesc" : $proddesc,"supp" : $suppid},function(response)
{
$lastid=response;
if($lastid != 0)
{
alert("Product has been saved with ID=" + $lastid);
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
}
});
if (remember.checked)
{
//alert($lastid);
$.post(baseurl+'index.php/main/newitem',{"prodlist" : $lastid ,"itlist" : 0,"itcost" : 0, "supp" : $suppid, "itname" : $prodname, "itunit" : "pc" },function(response)
{
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
//window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
}
}
非常感谢你 jQuery ajax函数($.ajax()
,$.get()
,$.post()
…)都返回一个对象,该对象封装了底层的jqXHR
对象
利用该函数链接两个异步调用:
//your first $.post :
$.post(...)
.then(function(){
if (remember.checked) {
//your second $.post :
$.post(...);
}
});
通过这种方式,仅当第一次调用成功时才会调用第二个函数(如果第一次调用导致错误,则不会触发任何操作)
您的第一个回调包含重定向:
window.location.href=baseurl+“index.php/main/mgtprods/?key=“+$suppid代码>。您必须更精确地选择何时执行此重定向
如果选中了记住
,则在第二次调用后触发重定向;如果未选中记住
,则在第一次调用后触发重定向,方法是链接延迟的
对象:
$.post(... /* remove the redirection from the first callback */)
.then(function(){
if (remember.checked) {
//your second $.post : return the Deferred corresponding to this call
return $.post(... /* remove the redirection from the second callback */);
} else {
// else, return the first Deferred object :
return this;
}
}).then(function(){
/* do your redirection here : */
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
试试这个
if($action=="save_prod") {
var remember = document.getElementById('chk');
var $suppid=document.getElementById('supp').value;
var $categlist=document.getElementById('categlist').value;
var $prodname=document.getElementById('prodname').value;
var $proddesc=document.getElementById('proddesc').value;
$.post(baseurl+'index.php/main/newproduct',{"categlist" : $categlist,"prodname" : $prodname," proddesc" : $proddesc,"supp" : $suppid},function(response) {
$lastid=response;
if (remember.checked) {
//alert($lastid);
$.post(baseurl+'index.php/main/newitem',{"prodlist" : $lastid ,"itlist" : 0,"itcost" : 0, "supp" : $suppid, "itname" : $prodname, "itunit" : "pc" },function(response) {
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
//window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
});
}
if($lastid != 0) {
alert("Product has been saved with ID=" + $lastid);
document.getElementById('resp').style.display='none';
document.getElementById('fade').style.display='none';
window.location.href=baseurl +"index.php/main/mgtprods/?key="+ $suppid;
}
});
}
我所做的就是将第二篇文章移动到第一篇文章的回调函数中。您也可以在第一篇文章中添加一个.done()
方法,然后在第二篇文章中添加第二个方法,但效果相同。在进行异步调用时,您无法预测哪一个是第一个。您可以使用延迟对象来设置承诺队列。检查或将第二个帖子放入第一个帖子的回调中。这不会执行第二个帖子:您的第一个回调重定向到一个新页面:window.location.href=baseurl+“index.php/main/mgtprods/?key=“+$suppid
,它将停止js执行并加载新页面。这似乎不会调用第二个帖子。。。你还有其他建议吗?我已经把第二篇文章移到了beforewindow.location.href
,所以它现在应该可以正常工作了。