jQuery ajax验证验证码


jQuery ajax validate captcha

我在发布验证码时遇到了一个问题。我将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';
?>

我将header设置为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进行通信。这是我的选择。

$.ajax({
    type: "POST",
    url: '/captcha_check.php',
    data: captcha_value,
    dataType: "text",
    success: function(data) {
        if(data == "true") {
            // correct
        } else {
            // nope
        }
    }
});
dataType: 'json', //Important:Sometimes JQuery fails to automatically detect it for you.
success: function(data) {
    console.log(data ? "Data is true" : "Data is false");
}