ajax post调用不会向jquery返回任何json响应

时间:2013-10-06 05:35:14

标签: javascript php jquery ajax json

我面临着ajax post类型调用的问题。当从jquery调用ajax调用时,我得到空白的php json响应。

下面是我正在使用的html,jquery和php代码,以便发送HTML表单数据并获取json响应。如果代码有任何问题或者是否需要对浏览器设置。

我正在使用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,deflate

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

内容长度52

Content-Type application / x-www-form-urlencoded;字符集= UTF-8

主持localhost

Origin null

User-Agent Mozilla / 5.0(Windows NT 6.2; WOW64; rv:24.0)Gecko / 20100101 Firefox / 24.0

响应标头

Connection Keep-Alive

内容长度85

Content-Type application / json

Date Sun,2013年10月6日04:48:54 GMT

Keep-Alive timeout = 5,max = 100

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

X-Powered-By PHP / 5.3.27

1 个答案:

答案 0 :(得分:-1)

希望可以为您提供帮助

jQuery代码

$("#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;  
    });
相关问题