AJAX POST 调用不会向 jQuery 返回任何 JSON 响应


ajax post call does not return any json response to jquery

我遇到了ajax帖子类型调用的问题。当从 jquery 调用 ajax 调用时,我得到空白的 php json 响应。

以下是我用来发送HTML表单数据并获得json响应的html,jquery和php代码。如果代码有任何问题,或者它必须对浏览器设置做一些事情,请告知。

我正在使用jQuery src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"。同样使用FirePHP插件,我能够看到表单输入被正确发送到php。

 HTML Code
 =========
  <form id = "frmLogin" action ="" autocomplete="off" style="width:10em;margin:0 auto" method="post">
   User Email ID : <input type = "email" name ="loginId" autocomplete = "off">              
  Password    : <input type = "password" name = "password" autocomplete = "off"> 
  <input id= "clkLogin" type="Submit" value="Submit" >  
  </form>
JQuery Code
===========
$("#frmLogin").submit(function() {
    $.ajaxSetup( { cache: false });      
    $.ajax( {     
        url: "http://localhost/validateUser.php" , 
        cache:false, 
        type:"POST",
        async:true, 
        data: $("form#frmLogin").serialize(),
        success:function(data){
                    $("#loginPage").hide();
                    $("#Registered").hide(); 
                    $("#userHomePage").show();
                                     $("button#user").html(data.firstName);     
            }, dataType:"json" 
           });
         return false;  
        });
 PHP Code
 ========
 <?php
 require_once('FirePHPCore/fb.php');
 $con = mysqli_connect("localhost","root","kpns@123","spa");
 if(mysqli_connect_errno()) {
echo "MYSQL connection error ::" . mysqli_connect_error();
 }
 $sql = "select * from spausers where email_id = '$_POST[loginId]' and pswd ='$_POST[password]' "; 
 fb($sql,'SQL Query'); // FirePHP console log shows sql statement with  the correct inputs sent from HTML form
 $result = mysqli_query($con,$sql);
 fb($result,'mysqli_query result');
 while ($row = mysqli_fetch_array($result)) {
 $data =    array ('emailid'=>$row['email_id'],'firstName' => $row['first_name'],'lastName' => $row['last_name']);  
 fb(json_encode($data),'mysqli_query fetch array'); // FirePHP console log shows result in json format {"key" : "value", "key":"value"}  
 }
 header("Content-Type: application/json");
 echo json_encode($data);
mysqli_close($con);
 ?>

请求标头

接受*/*

接受编码 gzip,放气

Accept-Language en-US,en;q=0.5

内容长度 52

Content-Type application/x-www-form-urlencoded; charset=UTF-8

主机

本地主机

原点空

User-Agent Mozilla/5.0 (Windows NT 6.2;哇64;rv:24.0( 壁虎/20100101火狐/24.0

响应标头

连接保持活动状态

内容长度 85

内容类型应用程序/json

日期 周日, 06 十月 2013 04:48:54 GMT

保持活动超时=5,最大值=100

服务器 Apache/2.2.25 (Win32( PHP/5.3.27

X-Powered-by PHP/5.3.27

希望能帮到你

JQuery Code

$("#frmLogin").submit(function() {
    // setup some local variables
    var $form = $(this);
    // Serialize the data in the form
    var serializedData = $form.serialize();
    $.ajaxSetup( { cache: false });      
    $.ajax( {  
        cache:false, 
        type:"POST",
        async:true, 
        dataType: "json",
        url: "http://localhost/validateUser.php",
        data: serializedData,
        success:function(data){
                $("#loginPage").hide();
                $("#Registered").hide(); 
                $("#userHomePage").show();
                $("button#user").html(data.firstName);     
        }
       });
     return false;  
    });