[英]ajax post call does not return any json response to jquery
I am facing issue with ajax post type call. 我面临ajax发布类型调用的问题。 I get blank php json response when an ajax call is invoked from jquery. 当从jquery调用ajax调用时,我得到空白的php json响应。
Below are the html, jquery and php code which i am using in-order to send HTML form data and get the json response.Please advice if there is any issue with the code or if it has got to do something with the browser settings. 以下是我正在使用的html,jquery和php代码,以发送HTML表单数据并获取json响应。请告知代码是否存在问题或浏览器设置是否有问题。
i am using jQuery src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js". 我正在使用jQuery src =“ http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js”。 Also using FirePHP addon, i am able to see the form inputs are correctly sent to php. 同样使用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);
?>
Accept *
/ *
接受*
/ *
Accept-Encoding gzip, deflate 接受编码gzip,放气
Accept-Language en-US,en;q=0.5 接受语言en-US,en; q = 0.5
Content-Length 52 内容长度52
Content-Type application/x-www-form-urlencoded; 内容类型应用程序/ x-www-form-urlencoded; charset=UTF-8 字符集= UTF-8
Host localhost 主机本地主机
Origin null 原点为空
User-Agent Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0 用户代理Mozilla / 5.0(Windows NT 6.2; WOW64; rv:24.0)Gecko / 20100101 Firefox / 24.0
Connection Keep-Alive 连接保持活动
Content-Length 85 内容长度85
Content-Type application/json 内容类型应用程序/ json
Date Sun, 06 Oct 2013 04:48:54 GMT 日期,2013年10月6日,星期日,格林尼治标准时间
Keep-Alive timeout=5, max=100 保持活动超时= 5,最大= 100
Server Apache/2.2.25 (Win32) PHP/5.3.27 服务器Apache / 2.2.25(Win32)PHP / 5.3.27
X-Powered-By PHP/5.3.27 X-Powered-by PHP / 5.3.27
Hope can help you 希望可以帮到你
$("#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;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.