如果前一个ajax的结果为true,则调用另一个ajax

call another ajax if the result of previous ajax is true

本文关键字:ajax true 另一个 调用 结果 如果 一个      更新时间:2023-09-26

如果前一个ajax的结果为true,我想调用另一个ajax
我在以前的ajax的成功函数中使用了if-else语句,
但在我的情况下,如果上一个ajax的结果是truefalse,那么下一个ajax仍然不是进程,只运行$('.captcha-error1').fadeIn(1000).delay(900).fadeOut();

PHP代码:

<?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");
  }
?>

这是我的javascript代码:

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

这是服务器端代码发出的:

if($datacode !== $datacaptcha){
    echo json_encode("salah");
}else{
    echo json_encode("benar");
}

这就是您的客户端代码正在寻找的:

if(hasil == 'true')

单词"benar"answers"true"对你来说可能意味着相同的东西,但它们是相同的字符串值。你的电脑不会为你翻译不同的语言。

检查您实际返回的字符串:

if(hasil == 'benar'){

不要使用字符串进行布尔检查,发送一个实际的布尔值:

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]);

在你的js:

success : function(hasil) {
    if(hasil.captchaMatch){
        ....

然而,这确实是不重要的,因为设计本身就被破坏了——任何机器人都可以直接发布到contact.php,从而绕过captcha。

相反,直接向contact.php发送一个ajax请求:

$('#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);
            }
        }
    });
});

在contact.php中,检查captcha代码并做出相应响应:

session_start();
$success = true;
$errors = [];
$datacode = $_SESSION["6_letters_code"];
$datacaptcha = $_POST['6_letters_code'];
if(!$datacode == $datacaptcha){
    $success = false;
    $errors[]= 'Captcha is wrong';
}
//the rest of your form handling code here.
//you can add other errors such as missing fields etc to the errors array
//then send response
header('Content-Type: application/json');
echo json_encode(['success'=> $success, 'errors'=>$errors]);

好的,我终于解决了这个问题。

这是我的validateCaptcha.hp:

<?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){...}解决的问题
感谢大家,你们是一位伟大的专家。