在这里呆了几天。使用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 }我在控制台中得到了这个结果。{“ nouser”:[“ noUserExist”]} 我的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} (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我只是想通了。我在print_r($_POST)之前是json_encode。 (adsbygoogle = window.adsbygoogle || []).push({});
10-06 00:03