2013-10-14 182 views
0

我有一個mysql查詢爲我的json數據創建這個輸出。但是我還沒有弄清楚如何訪問這個輸出中的屬性。訪問JSON(密鑰對/ MYSQL數組)中的嵌套對象

我已經嘗試使用data.objA,它返回undefined和objA] [0],我很茫然。任何援助將不勝感激。

JSON

({"objA":"yes","objB":[{"username":"ah2122","client_password":"288c0e42ab41faef3d1015e6fc299644","client_id":"36"}]}) 

PHP

<?php 
include 'init.php'; 

//get the posted values 
$user_name=htmlspecialchars($_POST['client_username'],ENT_QUOTES); 
$pass=md5($_POST['client_password']); 


//now validating the username and password 
$sql="SELECT username, client_password, client_id FROM clients WHERE username='".$user_name."'"; 

    $result = mysql_query($sql); 
     while($row = mysql_fetch_array($result)) 
     { 
      $rows[] = array(
      "username" => $row['username'], 
      "client_password" => $row['client_password'], 
      "client_id" => $row['client_id']); 
     } 

     //if username exists 
     if(mysql_num_rows($result)>0) 
     { 
      //compare the password 
      if(strcmp($rows[0]['client_password'],$pass)==0) 
      { 
       $SUCCESS="yes"; 
      } 
      else 
      $SUCCESS="no"; 
      } 
      else 
       $SUCCESS="no"; //Invalid Login 


$rows2 = array('objA' => $SUCCESS, 'objB' => $rows); 

$json = json_encode($rows2); 

$callback = $_GET['callback']; 
echo $callback.'('. $json . ')';  

    ?> 

SCRIPT

$(document).ready(function() 
{ 
    $("#client_login_form").submit(function() 
    { 
     //remove all the class add the messagebox classes and start fading 
     $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000); 
     //check the username exists or not from ajax 
     var cu = $("#client_username").val(); 
     var cp = $("#client_password").val(); 

     $.post("http://www.website.com/splash/cajax_login.php",{ client_username:cu,client_password:cp,rand:Math.random() } ,function(data) 
     {    
      if(data.objA=='yes') //if correct login detail 
      { 
      $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
      { 
       //add message and change the class of the box and start fading 
       $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1, 
       function() 
       { 
       window.localStorage["cusername"] = cu; 
       window.localStorage["cpassword"] = cp; 

       //redirect to secure page 
       document.location="#client_home"; 
       }); 

      }); 
      } 
      else 
      { 
      $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox 
      { 
       //add message and change the class of the box and start fading 
       $(this).html('Login Failed').addClass('messageboxerror').fadeTo(900,1); 
      });  
      } 

     }); 
     return false; //not to post the form physically 
    }); 
    //now call the ajax also focus move from 
    $("#client_password").blur(function() 
    { 
     $("#client_login_form").trigger('client_submit'); 
    }); 
+0

您的JSON是不是JSON ......你必須在開始和結束時以刪除括號。等等......它似乎是JSONP,你忘了交出CALLBACK參數。您應該添加一個if條件來檢查是否存在CALLBACK參數。如果沒有,你應該刪除圓括號 – devnull69

+0

www.website.com(或者你在這裏刪除的域名)與當前頁面在同一個域名,子域名,協議和端口上,或者你嘗試從外部服務器檢索JSON(P)? – devnull69

+0

該網站位於同一個域中,但最終產品將是使用跨域請求的移動應用。 – user2880254

回答

0

這可能是一個給你data.objA未定義

echo $callback.'('. $json . ')'; 

更改爲

echo $json; 

這應該發回只是JSON對象,而不是用$回調前面加上的。

+0

你可以在控制檯中檢查'typeof(data)'嗎?這很可能是一個字符串。你會想通過'JSON.parse(data)'將它轉換成對象' – meghamind

+0

是的,它做到了,現在一切都成功了,謝謝大家的幫助。 – user2880254

0

嘗試將此代碼

$callback = $_GET['callback']; 
echo $callback.'('. $json . ')'; 

改變

if(isset($_GET['callback'])) { 
    echo $_GET['callback'].'('. $json . ')'; 
} else { 
    echo $json; 
}