如何使用Jquery AJAX从PHP json_encode读取JSON数据

时间:2014-04-30 22:16:20

标签: javascript php jquery ajax json

已经好几天了。使用jquery ajax执行某些操作并尝试从我的服务器获取json编码的响应。不知道为什么现在这对我有用。

以下是我的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
    }

我在我的控制台中得到了这个结果。 {&#34; nouser&#34; :[&#34; noUserExist&#34;]}&lt;&lt;&lt; -----我如何抓住这个。

我的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
}

4 个答案:

答案 0 :(得分:3)

我只是想通了。我在print_r($_POST)之前json_encode

答案 1 :(得分:1)

您需要指定将json对象发送回浏览器,然后在PHP代码中回显它。

header("Content-type: application/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'
    });

答案 2 :(得分:0)

根据您返回的结果判断{ "nouser" : ["noUserExist"] } - 注意方括号 - 您想要的值存储在数组中,因此您需要:

console.log(data.nouser[0]);

答案 3 :(得分:0)

如果console.log(data.nouser);语句正在返回

{ "nouser" : ["noUserExist"] }

然后你可以尝试

console.log(data.nouser[0]);

应返回noUserExist