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 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器