如果前一个ajax的结果为true,则调用另一个ajax
call another ajax if the result of previous ajax is true
如果前一个ajax的结果为true,我想调用另一个ajax
我在以前的ajax的成功函数中使用了if-else语句,
但在我的情况下,如果上一个ajax的结果是true或false,那么下一个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){...}
解决的问题
感谢大家,你们是一位伟大的专家。
相关文章:
- 挂钩到Rails内置远程:true'ajax:成功'事件
- 通过AJAX从具有LazyLoadingEnabled=true的EF返回的数组不正确
- CORS-如果Jquery-ajax's”;withCredentials:true”;已使用
- Jquery-(ajax async:true)元素大小没有响应
- 使用自定义 ajax 请求渲染 js 部分(而不是远程:true)
- Rails 3 在 onclick 上创建一个
做一个 ajax 与 :remote => true 链接相同 - While ajax, async:true,deferred
- 一个带有 async:true 的 ajax 函数
- 如何排序这个jQuery tha运行ajax然后必须返回true
- 获取 Ajax 函数以返回 true 或 false
- 带有 ajax 远程 true 的 Rails link_to 不更新视图
- 如何从AJAX响应中查找值(true或false)
- 是否可以将true/false值传递给ajax函数
- 如果前一个ajax的结果为true,则调用另一个ajax
- 当async为true时,Ajax调用返回undefined
- 即使禁用了XMLHTTP支持,jQuery.support.ajax也返回true
- php发布后AJAX重定向返回true或无错误
- Remote true on button_to not进行ajax调用
- Ajax总是返回true
- Rails AJAX remote:带动态id的true