访问 JSON 中的嵌套对象(密钥对/MYSQL 数组)
Accessing Nested Objects in JSON (Key Pairs/MYSQL Array)
我有一个 mysql 查询为我的 json 数据创建此输出。 但是,我无法弄清楚如何访问此输出中的属性。
我尝试使用返回undefined和objA][0]的data.objA,并且不知所措。 任何协助将不胜感激。
杰伦
({"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 . ')';
?>
脚本
$(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');
});
这可能是给你data.objA undefined的那个
echo $callback.'('. $json . ')';
更改为
echo $json;
这应该只发回 json 对象,而不是在它前面加上 $callback。
尝试更改此代码
$callback = $_GET['callback'];
echo $callback.'('. $json . ')';
自
if(isset($_GET['callback'])) {
echo $_GET['callback'].'('. $json . ')';
} else {
echo $json;
}
相关文章:
- 如何在javascript中获取数组密钥对的json子值
- html keygen替代方案,在浏览器中生成密钥对
- 访问 JSON 中的嵌套对象(密钥对/MYSQL 数组)
- 在Javascript中生成PGP密钥对,并使用加密的PGP私钥对文本进行签名
- OpenPGP.min.js 不会生成密钥对
- JSON 数据加密/解密或密钥值对
- 在 PHP 可读的 JavaScript 中创建 PEM 密钥对
- 如何在ajax javascript中获取数据类型json的特定值-密钥对
- 从json(不在正确的密钥对中)创建javascript对象(带有密钥对)
- 使用d3或jquery的json对象中有多少不同的密钥对值
- java.lang.RuntimeException:无法生成DH密钥对
- 在嵌套json对象中查找和更新,而不更改不同子路径中的相同密钥对值
- 如何使用Web Crypto生成密钥对,并在Firefox AddOn中访问其密钥
- 使用javascript将数组存储为json中的密钥对
- 不能使用密钥对验证Node.js加密中的签名
- jquery对象密钥对未定义
- 在MapReduce MongoDB中按密钥对数据进行分组
- Angularjs:从密钥对中解析值
- 如何返回仅包含一个特定密钥对的多个集合
- Ng-options带有一个密钥对对象数组