2013-03-13 32 views
2

的价值,这是在PHP代码无法从JSON

<?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);  
?> 

这是HTML代码

    $.ajax({ 
       type: "get", 
       url: "http://mydomain.com/ajax_login.php", 
       data: { 
        user_name: $("#username").val(), 
        password: $("#password").val() 
       }, 
       success: function(jsondata){ 

        if (jsondata.flag == "0"){ 
         //if flag is 0, indicate login fail 
        } 
        else { 
         //if flag is 1, indicate login success and redirect to another page 
         window.location.href = 'ABC.html'; 
        }      
       }, 
       datatype: "json" 
      }); 

显示屏显示"{\"flag\":\"0\",\"msg\":\"Incorrect id.\"}"

我的问题是,现在连标志是0,它仍然会转到ABC.html 如果要修改if子句,那么如果标志为0,它仍然会在子句的真实部分?

EDITED这是编码

+2

你期待这段代码做什么?你正在创建一个'$ message'数组,然后'json_encode'然后'echo'出来。这正是你得到的。 – Steve 2013-03-13 22:08:37

+1

请务必在jQuery ajax方法中用'dataType:“json”'配置您的ajax调用,并确保在您的PHP响应中发送JSON头。 – MatRt 2013-03-13 22:09:55

+0

这似乎对我很好。你想要它做什么? – Dave 2013-03-13 22:10:29

回答

1

务必在您dataType: "json"配置您的Ajax调用jQuery的AJAX方法,并且一定要发送JSON头在喜欢你的PHP回应。

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调用,结果可能被解释为一个简单的字符串。

+0

正确的标题 - 是一个不错的音符。你提到它很好,我忘了。 – 2013-03-13 22:21:34

+0

我不知道标题的重要性。添加后,我的问题是固定的。感谢所有 – HUNG 2013-03-13 22:26:42

+0

@ user1073122所以你得到我的+1,祝贺,晚安)! – 2013-03-13 22:38:03

1

也许jsondata.flag是未定义的更多细节。
您需要解码响应字符串以将其用作JS对象。
或者设置dataType : 'json' ...

+0

对不起,但JSON解析是自动使用ajax jquery。无需解码任何东西,只需要正确配置即可。 – MatRt 2013-03-13 22:18:02

+0

@ user1073122他没有将其配置为自动分析。 – 2013-03-13 22:20:30

+0

所以最好的答案是正确配置,不要自己解码。 – MatRt 2013-03-13 22:21:11