jQuery ajax验证验证码

时间:2011-04-26 10:21:52

标签: php jquery ajax captcha

发布验证码以通过php验证时出现问题。 我将captcha_value字符串发送到captcha_check.php,我不知道如何检索返回值'true'或'false'

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       data: captcha_value
       success: function(data) {
          **?WHAT TO DO HERE? how to get true or false**
       }
});

captcha_check.php
<?php   

if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
?>

3 个答案:

答案 0 :(得分:2)

我将标题设置为输出为xml。

captcha_check.php

<?php   
header('Content-Type:text/xml');//needed to output as xml(that is my choice)
echo "<root><message>";
if ($_POST['captcha'] == $_SESSION['captcha'])
echo 'true';
else
echo 'false';
echo "</message></root>";
?>

$("#myForm").submit(function() {
$.ajax({
       type: "POST",
       url: '/captcha_check.php',
       dataType:'xml', 
       data: captcha_value
       success: function(data) {
          if($(data).find('message').text() == "true"){
             //now you get the true. do whatever you want. even call a function
            }
          else{
        //and false
          }
       }
});

这是我的解决方案也可能对你有用。我总是喜欢用xml进行交流。多数民众赞成我的选择

答案 1 :(得分:1)

$.ajax({
    type: "POST",
    url: '/captcha_check.php',
    data: captcha_value,
    dataType: "text",
    success: function(data) {
        if(data == "true") {
            // correct
        } else {
            // nope
        }
    }
});

答案 2 :(得分:1)

dataType: 'json', //Important:Sometimes JQuery fails to automatically detect it for you.
success: function(data) {
    console.log(data ? "Data is true" : "Data is false");
}
相关问题