[英]how to read JSON data from PHP json_encode using Jquery AJAX
BEEN at this for days. 在这里呆了几天。 Using jquery ajax to perform somethings and trying to get a json encoded response from my server. 使用jquery ajax执行某些操作并尝试从我的服务器获取json编码的响应。 Not sure why this is now working for me. 不知道为什么现在对我有用。
Below is my Javascript function 以下是我的Javascript函数
function checkFriendsEmail(friendsEmail){
var request = $.ajax({
url : 'http://localhost/loginsentology/serverside/checkfriendexist2.php',
type: 'POST',
data: {
'checkfriend' : 'checkfriend',
'friendsemail' : friendsEmail,
},
success: function(data) {
console.log(data); <<<-- Comes back to my console as a JSON obj
console.log(data.nouser); <<-- Comes back undefined
}
I get this result in my console. 我在控制台中得到了这个结果。 { "nouser" : ["noUserExist"] } <<<----- How do I grab this. {“ nouser”:[“ noUserExist”]} <<< -----我该怎么抓。
MY PHP is below 我的PHP低于
$fdarray = array();
$_db = DB::getInstance();
$_query = $_db->query('SELECT * FROM users WHERE email = ? ', array($_POST['friendsemail']));
$results = $_query->results();
$numberOfRows = $_db->numberOfRows();
if ($numberOfRows == 0) {
//$noUserExist = 'NoUserExist';
$fdarray['nouser'] = array();
array_push($fdarray['nouser'], 'noUserExist');
echo json_encode($fdarray);
return false;
}
else {
//friends email exist in users db --> now we must check to see if friend has devices
$friendsEmail = $results[0]->email;
if ($_POST['friendsemail'] == $friendsEmail) {
$id = $results[0]->id;
$_db2 = DB::getInstance();
$_query2 = $_db2->query('SELECT * FROM devices WHERE userid = ? ', array($id));
$results2 = $_query2->results();
$numberOfRows2 = $_db2->numberOfRows();
if ($numberOfRows2 == 0) {
//user has no devices attached to name
$fdarray['userexist'] = array();
$fdarray['nodevices'] = array();
array_push($fdarray['userexist'], true);
array_push($fdarray['nodevices'], 'noDevicesForUser');
echo json_encode($fdarray);
return false;
}
else {
$fdarray['userexist'] = array();
$fdarray['devices'] = array();
array_push($fdarray['userexist'], true);
for ($i = 0; $i < $numberOfRows2; $i++) {
array_push($fdarray['devices'], $results2[$i]->devicename);
}
//end for statement
}
//end number of rows2
echo json_encode($fdarray);
}
//end firendsemail == firendsemail
}
I just figured it out. 我只是想通了。 I was print_r($_POST)
before the json_encode
. 我在json_encode
之前是print_r($_POST)
。
You need to specify you are sending json object back to the browser before echo it out in your php code. 您需要指定将JSON对象发送回浏览器,然后才能在PHP代码中将其回显。
header("Content-type: application/json");
In you ajax function, you also want to specify the dataType is json 在您的ajax函数中,您还想指定dataType为json
var request = $.ajax({
url : 'http://localhost/loginsentology/serverside/checkfriendexist2.php',
type: 'POST',
data: {
'checkfriend' : 'checkfriend',
'friendsemail' : friendsEmail,
},
success: function(data) {
console.log(data); //<<<-- Comes back to my console as a JSON obj
console.log(data.nouser); //<<-- Comes back undefined
console.log(data.nouser[0]);
},
dataType: 'json'
});
根据返回的结果来判断{ "nouser" : ["noUserExist"] }
-注意方括号-所需的值存储在数组中,因此您需要:
console.log(data.nouser[0]);
If the console.log(data.nouser);
如果console.log(data.nouser);
statement is returning 声明正在返回
{ "nouser" : ["noUserExist"] }
Then you can try 那你可以试试
console.log(data.nouser[0]);
which should return noUserExist
. 应该返回noUserExist
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.