Ajax中的成功函数;使用Json时不起作用


The success Function in Ajax Doesn't work When using Json

我有一个登录表单,显示在jquery的对话框中我需要使用Ajax&json方法问题是ajax中的成功函数不起作用,我总是得到错误函数

这是我的php和jquery代码:

Jquery:


    $(document).ready(function () {
    $(".logbut").click(
         function () {
        $(".login").dialog({
              width: 400,
              height:320,
        modal: true,    
        autoOpen: true,
        resizable: false,
        show: {
          effect: "blind",
          duration: 3000
        },
         hide: {
          effect: "drop", 
          duration: 1000
        },
            buttons: {
                   'login': function() {
                       $.ajax({
                           url:"login.php",
                           type: 'POST',
                           dataType:"json",

                           success: function(data) {
                               if(data.status == 'success'){
                               alert("Thank you for subscribing!");
                               }else if(data.status == 'error'){
                               alert("Error on query!");
                               }
                           },
                           error: function(){
                            alert("the ajax doesnt work");   
                           }
                   })
                   }
            }
        })
         })
});

login.php:


  <?php

  $serverName = "AMIN-PC";

  $connectionInfo = array( "Database"=>"Market");
  $conn = new PDO("sqlsrv:Server=AMIN-PC;Database=Market");
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $response_array = array();
  if( $conn === false ) {
  die( print_r( sqlsrv_errors(), true));
 }

  $passorg =  $_REQUEST['passorg'];
  $passorg = $conn->quote($passorg); 
  $userorg =  $_REQUEST['userorg'];
  $userorg = $conn->quote($userorg);

   $sql = "select count(*) from customers
    where customers_user=$userorg
    and customers_pass=$passorg";
   $stmt = $conn->query($sql);
   $result = $stmt->fetch();
   $count = $result[0];

  if($count >0) {
  $response_array['status'] = 'success'; 
  }
   else{
  $response_array['status'] = 'error'; 
 }
  header('Content-type: application/json');
 echo json_encode($response_array);
  ?>

HTML:


  <div class="logbut">Login</div>

  <div class="login" style="display:none;">
  <form method="post" class="logform" action="login.php">
  <input type="text" name="userorg" style=" width:80px;" class="userorg" />
  <label>Username</label>

  <input type="password" name="passorg"  style=" width:80px;" class="passorg"/>
  <label>Password</label>
 <input type="submit" style="display:none" value="check"/>
 </form>
 </div>

感谢

您可以尝试在ajax请求中指定如下数据参数吗:

$.ajax({
  url:"login.php",
  type: 'POST',
  dataType:"json",
  data:{passorg:value of passorg},
  success:.... 

您需要在ajax中传递变量,并在php中获取这些变量,例如:

$.ajax({
     url: "your url",
     type: "POST",
     cache: true,
     dataType:'json',
     data: {id: cid},
     success: function(returnedData){
       console.log(returnedData);
     },
     error: function (xhr, tst, err) {
       console.log(err);
     } 
 });

PHP

 $_REQUEST['id'];