如果前一个ajax的结果为真,我想调用另一个ajax
我在之前的ajax的成功函数中使用了 if else 语句,
但在我的情况下,如果以前的ajax的结果是 true 或 false ,然后下一个ajax仍然不是进程,只需运行<?php
session_start();
$datacode = $_SESSION["6_letters_code"];
$datacaptcha = $_POST['6_letters_code'];
if($datacode !== $datacaptcha){
echo json_encode("false");
}else{
echo json_encode("true");
}
?>
。
PHP代码:
var captchaCode;
captchaCode = $('#6_letters_code').serialize();
$.ajax({
type : "POST",
url : "php/captcha/validateCaptcha.php",
data : captchaCode,
success : function(hasil) {
if(hasil == 'true'){
console.log("if is "+hasil);
$.ajax({
type : "POST",
url : "php/contact.php",
data : contactForm.serialize(),
success : function(result) {
if (result == 'true') {
contactForm.stop().animate({opacity : '0'}, 400, function() {
contactForm.css('display', 'none');
$('#success').css('display', 'block');
$('#success').stop().animate({opacity : '1'}, 900);
});
} else {
$('#error').css('display', 'block');
$('#error').stop().animate({opacity : '1'}, 1000);
alert('Error Message: ' + result);
}
},
error : function(xmlHttpRequest, textStatus, errorThrown) {
$('#error').css('display', 'block');
$('#error').stop().animate({opacity : '1'}, 1000);
alert(errorThrown);
}
});//ajax II
}else{
console.log("else is "+hasil);
$('.captcha-error1').fadeIn(1000).delay(900).fadeOut();
}
}
});//ajax I
这是我的javascript代码:
{{1}}
答案 0 :(得分:1)
这是您的服务器端代码发出的内容:
if($datacode !== $datacaptcha){
echo json_encode("salah");
}else{
echo json_encode("benar");
}
这就是您的客户端代码所寻求的:
if(hasil == 'true')
字样&#34;贝纳&#34;和&#34;真&#34;可能对你来说意味着同样的事情,但它们不是相同的字符串值。您的计算机不会为您翻译语言。
检查您实际返回的字符串:
if(hasil == 'benar'){
答案 1 :(得分:1)
不要使用字符串进行布尔检查,发送实际的布尔值:
{
name: 'Rohan Dalvi',
externalLinks: {
website: 'mywebsite'
}
}
在你的js中:
session_start();
$datacode = $_SESSION["6_letters_code"];
$datacaptcha = $_POST['6_letters_code'];
//set correct header
header('Content-Type: application/json');
echo json_encode(['captchaMatch'=> $datacode==$datacaptcha]);
然而,这实际上是无关紧要的,因为设计的自我被打破 - 任何机器人都可以直接发布到success : function(hasil) {
if(hasil.captchaMatch){
....
,从而绕过验证码。
而是将一个ajax请求直接发送到contact.php:
contact.php
在contact.php中,检查验证码并做出相应的回应:
$('#yourformid').submit(function(ev){
ev.preventDefault();
$.post('php/contact.php', $(this).serialize(), function(response){
if(response.success){
//show thanks message, hide form etc
}else{
$.each(response.errors, function(index, error){
//show errors
console.log(error);
}
}
});
});
答案 2 :(得分:0)
好的,最后我解决了这个问题。
这是我的validateCaptcha.php:
<?php
session_start();
$datacode = $_SESSION["6_letters_code"];
$datacaptcha = $_POST['6_letters_code'];
$captchaResponse = 'true';
try{
if($datacode !== $datacaptcha){
$captchaResponse = 'false';
}
echo json_encode($captchaResponse);
}catch(Exception $e){
echo $e;
}
?>
在我的js中没有编辑。
使用try{....}catch(e){...}
确定的问题
对所有人来说,你是伟大的专家。