2013-03-13 34 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