Ajax php GET方法不起作用?

Ajax php GET方法不起作用?,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我试图通过ajax验证captcha,但它只会给我无效的captcha消息 这是我的ajax <script> $(document).ready(function(){ $('#submitcap').click(function(){ $.post("report.php", $("#formcap").serialize(), function(response) { $('#error').html(respo

我试图通过ajax验证captcha,但它只会给我无效的captcha消息

这是我的ajax

<script>
   $(document).ready(function(){

      $('#submitcap').click(function(){

         $.post("report.php", $("#formcap").serialize(),  function(response) {
            $('#error').html(response);
         });
         return false;

      });
   });
</script>

$(文档).ready(函数(){
$(“#提交映射”)。单击(函数(){
$.post(“report.php”,$(“#formcap”).serialize(),函数(响应){
$('#error').html(响应);
});
返回false;
});
});
这是我的html

<span id="error"></span>
<form method="GET" id="formcap">
   <img src="<?php echo $baseUrl; ?>/captcha/captcha.php" id="captcha" /><br/>

   <!-- CHANGE TEXT LINK -->
   <a href="#" onclick="
    document.getElementById('captcha').src='<?php echo $baseUrl; ?>/captcha/captcha.php?'+Math.random();
    document.getElementById('captcha-form').focus();"
    id="change-image">Not readable? Change text.</a><br/><br/>

   <input type="text" name="captcha" id="captcha-form" style="width:100px" autocomplete="off" /><br/>
   <input type="submit" id="submitcap"/>
</form>

/captcha/captcha.php“id=“captcha”/>



这是我的PHP文件

<?php
   /** Validate captcha */
   if (!empty($_REQUEST['captcha'])) {
      if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) !=$_SESSION['captcha']) {
         $captcha_message = "Invalid captcha";
         $style = "background-color: #FF606C";
      } else {
         $captcha_message = "Valid captcha";
         $style = "background-color: #CCFF99";
      }

      $request_captcha = htmlspecialchars($_REQUEST['captcha']);

      echo <<<HTML
      <div id="result" style="$style">
          <h2>$captcha_message</h2>
          <table>
          <tr>
             <td>Session CAPTCHA:</td>
             <td>{$_SESSION['captcha']}</td>
          </tr>
          <tr>
             <td>Form CAPTCHA:</td>
             <td>$request_captcha</td>
          </tr>
       </table>
     </div>
HTML;
     unset($_SESSION['captcha']);
   }
?>


我不知道为什么我会收到无效的验证码消息,如果我在没有ajax的情况下验证此验证码,它工作正常

我想其他人得到的是,当您应该使用$\u POST时,您使用了$\u请求,因为您通过使用jQuery.POST函数将数据发布到此表单

您需要做的是调试接收发布数据的PHP代码。 因为您无法将该代码中的数据回显到屏幕上,所以请尝试将其写入一个文件,以便在每次执行后进行检查

<?php
  /** Validate captcha */

  // show whats actually in the session file as this point
  file_put_contents( 'captcha.log', 'SESSION ' . print_r($_SESSION, TRUE) );   

  // show what you passed to the script
  file_put_contents( 'captcha.log', 'POST ' . print_r($_POST, TRUE), FILE_APPEND );  

  /*
   * Now you can edit the captcha.log file which will be in the same directory as this script
   */


  if (!empty($_POST['captcha'])) {
     if (empty($_SESSION['captcha']) || trim(strtolower($_POST['captcha'])) != $_SESSION['captcha']) {
        $captcha_message = "Invalid captcha";
        $style = "background-color: #FF606C";
     } else {
        $captcha_message = "Valid captcha";
        $style = "background-color: #CCFF99";
     }

....

你尝试了什么?所以这不是一个调试工具。你知道
post
get
之间的区别,不是吗?你说我尝试的是什么意思?这就是我想要的代码tried@Leri是的,我知道,但我不知道如何在AJAX中使用get方法:(@YousafEhsan)您在这里不是直接使用xhr,而是使用某种包装器(更可能是
jQuery
)。那么看看那个库的文档。如果是jQuery,它就是。无论如何,-1表示没有研究。嗨,谢谢你的解决方案,我唯一忘记的是
session_start();
,因为如果我们不知道会话验证码是什么,我们如何验证..我正在使用你的建议解决方案..再次感谢