Php 无法从JSON获取值
这是php中的代码Php 无法从JSON获取值,php,html,ajax,json,Php,Html,Ajax,Json,这是php中的代码 <?php session_start(); //Connect to database from here $link = mysql_connect('****', '****', '****'); if (!$link) { die('Could not connect: ' . mysql_error()); } //select the database | Change the name of d
<?php session_start();
//Connect to database from here
$link = mysql_connect('****', '****', '****');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//select the database | Change the name of database from here
mysql_select_db('****');
//get the posted values
$user_name=htmlspecialchars($_GET['user_name'],ENT_QUOTES);
$pass=$_GET['password'];
//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
//if username exists
if(mysql_num_rows($result)>0)
{
//compare the password
if(strcmp($row['password'],$pass)==0)
{
// Return success message
$message = array("flag" => "1", "msg" => "Login successfully.");
echo json_encode($message);
//Regenerate session ID to prevent session fixation attacks
//session_regenerate_id();
//now set the session from here if needed
//$_SESSION['u_name']=$user_name;
//$member=mysql_fetch_assoc($result);
//$_SESSION['u_id']=$member['id'];
//$name_show=$member['first_name'].' '.$member['last_name'];
//$_SESSION['name']=$name_show;
//Write session to disc
//session_write_close();
}
else
// Return error message
$message = array("flag" => "0", "msg" => "Incorrect password.");
echo json_encode($message);
}
else //if username not exists
{ // Return error message
$message = array("flag" => "0", "msg" => "Incorrect id.");
echo json_encode($message);
}
mysql_close($link);
?>
显示屏显示“{\”标志\:\”0\“,\”消息\“:\”不正确的id.\“}”
我的问题是,现在即使标志是0,它仍然指向ABC.html
如果要修改if子句,使其在标记为0时仍处于子句的真实部分,应如何修改
编辑这是编码的更多细节可能
jsondata。标志
未定义。您需要解码响应字符串以将其用作JS对象。
或者在jQuery ajax方法中设置
dataType:'json'
…确保使用dataType:“json”
配置ajax调用,并确保在PHP响应中发送json头
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/json");
echo json_encode($yourArray);
如果您没有正确配置ajax调用,结果可能会被解释为一个简单的字符串。您希望这段代码做什么?您正在创建一个
$message
数组,然后对其进行json\u编码
然后将其输出。这正是您得到的结果。请确保在jQuery ajax方法中使用数据类型:“json”
配置ajax调用,并确保在PHP响应中发送json头。这对我来说似乎工作正常。你想让它做什么?还有,这就是你所有的代码吗?我相信代码的第二部分还有更多内容,尤其是在成功之前。请发布完整的代码。我希望依赖标志1或0,并决定执行不同的工作。很抱歉,使用ajax jquery可以自动进行JSON解析。无需解码任何内容,只需正确配置。@user1073122他没有将其配置为自动解析。正确的标题-是一个很好的注释。很好,你提到了,我忘了。我不知道头球的重要性。添加它之后,我的问题就解决了。谢谢all@user1073122所以你得到了我的+1,恭喜你,晚安)!
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
header("Content-type: application/json");
echo json_encode($yourArray);