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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!