Javascript 如何使用Jquery AJAX从PHP JSON_encode读取JSON数据
我已经做了好几天了。使用jQueryAjax执行某些操作,并尝试从我的服务器获取json编码的响应。不知道为什么这对我有用 下面是我的Javascript函数Javascript 如何使用Jquery AJAX从PHP JSON_encode读取JSON数据,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我已经做了好几天了。使用jQueryAjax执行某些操作,并尝试从我的服务器获取json编码的响应。不知道为什么这对我有用 下面是我的Javascript函数 function checkFriendsEmail(friendsEmail){ var request = $.ajax({ url : 'http://localhost/loginsentology/serverside/checkfriendexist2.php', type: 'POST'
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
}
函数checkFriendsMail(FriendsMail){
var请求=$.ajax({
网址:'http://localhost/loginsentology/serverside/checkfriendexist2.php',
键入:“POST”,
数据:{
“checkfriend”:“checkfriend”,
“friendsemail”:friendsemail,
},
成功:函数(数据){
console.log(数据);results();
$numberOfRows2=$\u db2->numberOfRows();
如果($numberOfRows2==0){
//用户没有附加到名称的设备
$fdarray['userexist']=array();
$fdarray['nodevices']=array();
array_push($fdarray['userexist'],true);
array_push($fdarray['nodevices'],'nodevicsforuser');
echo json_编码($fdarray);
返回false;
}
否则{
$fdarray['userexist']=array();
$fdarray['devices']=array();
array_push($fdarray['userexist'],true);
对于($i=0;$i<$numberOfRows2;$i++){
阵列推送($fdarray['devices',$results2[$i]->devicename);
}
//结束声明
}
//结束行数2
echo json_编码($fdarray);
}
//end firendsemail==firendsemail
}
根据返回的结果判断,{“nouser”:[“noUserExist”]}
-注意方括号-所需的值存储在数组中,因此需要:
console.log(data.nouser[0]);
根据返回的结果判断,
{“nouser”:[“noUserExist”]}
-注意方括号-所需的值存储在数组中,因此需要:
console.log(data.nouser[0]);
如果console.log(data.nouser);
语句正在返回
{ "nouser" : ["noUserExist"] }
那你可以试试
console.log(data.nouser[0]);
如果console.log(data.nouser);
语句正在返回,则应返回nouser-exist
{ "nouser" : ["noUserExist"] }
那你可以试试
console.log(data.nouser[0]);
它应该返回
noUserExist
我刚刚计算出来。在json\u编码之前我是print\r($\u POST)
,我刚刚计算出来。我是print\r($\u POST)
在json\u编码之前
您需要指定将json对象发送回浏览器,然后在php代码中回显它
header("Content-type: application/json");
在ajax函数中,还需要指定数据类型为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'
});
var请求=$.ajax({
网址:'http://localhost/loginsentology/serverside/checkfriendexist2.php',
键入:“POST”,
数据:{
“checkfriend”:“checkfriend”,
“friendsemail”:friendsemail,
},
成功:函数(数据){
console.log(data);//在php代码中回显json对象之前,需要指定将json对象发送回浏览器
header("Content-type: application/json");
在ajax函数中,还需要指定数据类型为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'
});
var请求=$.ajax({
网址:'http://localhost/loginsentology/serverside/checkfriendexist2.php',
键入:“POST”,
数据:{
“checkfriend”:“checkfriend”,
“friendsemail”:friendsemail,
},
成功:函数(数据){
console.log(data);//如果在ajax调用中添加dataType:“json”
是否有效?如果添加dataType:“json”,是否有效,
到ajax调用?显然您不能打印json编码之上的任何内容显然您不能打印json编码之上的任何内容感谢数据类型:“json”
感谢数据类型:“json”