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

时间:2015-04-07 09:20:29

标签: php jquery ajax json

我有一个登录表单,显示在jquery的对话框中 我需要使用Ajax&将一个变量从php传递给我的jquery。 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>

由于

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

你需要在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'];