两个$的other.Second $。员额请求后。员额要求人们不得到执行 [英] Two $.post requests one after the other.Second $.post request doesn't get executed
问题描述
我有一个JavaScript函数中,我有两个$。员额requests.Both的$。员额的要求应该得到executed.But有时,第二个$。员额的要求没有得到executed.What可能是它的原因?
I have a javascript function inside which i have two $.post requests.Both the $.post requests should get executed.But sometimes,second $.post request doesn't get executed.What could be the reason for it?
推荐答案
$。员额()
是$。阿贾克斯()结构的缩写形式。我通常preFER使用 $阿贾克斯()
结构,因为:
$.post()
is an abbreviated form of the $.ajax() structure. I usually prefer to use the $.ajax()
structure because:
- 这很容易看出,如果我错过了什么
- 我可以更容易地添加额外的参数,可以如
异步:假的,
- 在当前新的阿贾克斯,我发现这相当容易解决这个结构
在你的情况,你可能会发现你的问题更容易解决了 $。阿贾克斯()
结构,因为它会更容易看到的第二Ajax调用(取决于第一个Ajax调用的结果)必须在第一个ajax调用成功功能发生的。
In your case, you might find your problem easier to solve in a $.ajax()
structure, since it would be easier to see that a second ajax call (that depends on the outcome of a first ajax call) must happen in the success function of the first ajax call.
下面是一个独立的例子(太糟糕的jsfiddle无法处理阿贾克斯...):
Here is a standalone example (too bad jsFiddle cannot handle ajax...):
TESTER.PHP
TESTER.PHP
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#eml').focus();
$('#mybutt').click(function() {
var $a = $('#eml').val();
var $b = $('#pw').val();
//alert('Email: ' +$a+ ' Pass: ' +$b);
$.ajax({
type:"POST",
url: "yourphpfile.php",
data: 'email=' +$a+ '&pass=' +$b,
success: function(data) {
alert(data);
var aData = data.split('|');
var name = aData[0];
var code = aData[1];
alert('Name: ' +name+ ' Code: ' +code);
$.ajax({
type:"POST",
url: "yourphpfile.php",
data: 'name=' +name+ '&code=' +code,
success: function(newdata) {
alert(newdata);
} //END success_ajax2
}); //END ajax() #2
} //END success_ajax1
}); //END ajax() #1
}); //END mybutt.click()
}); //END $(document).ready()
</script>
</head>
<body>
Email: <br />
<input type="text" id="eml" /><br />
Password: <br />
<input type="password" id="pw" /><br />
<input type="button" id="mybutt" value="Submit">
</body>
</html>
yourphpfile.php
<?php
if (isset($_POST['email'])) {
$e = $_POST['email'];
$p = $_POST['pass'];
$name = 'Bob';
$code = '1234';
$resp = $name .'|'. $code;
echo $resp;
}else if (isset($_POST['name'])) {
$n = '<h1>Here is something new</h1>';
echo $n;
}
这篇关于两个$的other.Second $。员额请求后。员额要求人们不得到执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!