dongyue9860 2014-04-30 22:16 采纳率: 100%
浏览 274
已采纳

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

BEEN at this for days. Using jquery ajax to perform somethings and trying to get a json encoded response from my server. Not sure why this is now working for me.

Below is my Javascript function

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.

MY PHP is below

$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条回答 默认 最新

  • dongping4273 2014-04-30 22:25
    关注

    I just figured it out. I was print_r($_POST) before the json_encode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到