我需要创建像{"groupslist":{"groupname":["user1","user2","user3"],"groupname2":["user1","user2","user3"],"groupname3":["user1","user2","user3"]}}
这样的数组,其值是从对服务器的不同ajax调用获得的,在第一次ajax调用中,我得到grouplists,如groupname,groupname2等,并在成功函数中我做了ajax调用每个groupname并获取特定组名的用户并创建数组并将其推送到主grouplist,输出结果不正确我想我必须使用一些回调才能使其正确,请帮帮我并告诉我`
$.ajax({
url: 'get_group_list',
type: 'POST',
success: function (data) {
groupnamess=data.replace("[","").replace(/"/g, "").replace("]","").split(',');
console.log("groupnameeeee",groupnamess);
for(var i in groupnamess)
{
grup[i]=groupnamess[i];
userss.push({
label:groupnamess[i],
value:groupnamess[i]
});
grupss.push(groupnamess[i]);
}
for(var i in grupss)
{
console.log("geeeeeeeeeeeeeeeeeeeeeeeeee",grupss);
console.log("geeeeeeeeeeeeeeeeeeeeeeeeee",groupnamess[i]);
$.ajax({
url: 'get_user_list',
type: 'POST',
data:{'name':groupnamess[i]},
success: function (data) {
console.log("55555555555555555555"+data);
if(data!='[]')
{
//addy.substr(0, addy.indexOf('.'));
var group=groupnamess[i];
console.log("cccccccccccccccccccccc"+group);
grouplist[group]=new Array();
console.log("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"+data);
var usrs=data.replace("[","").replace(/"/g, "").replace("]","").split(',');
for(var j in usrs)
{
grouplist[group].push(usrs[j]);
console.log("rrrrrrrrrrrrrrr"+usrs[j]);
}
console.log('ooooooooooooooooooooooooooo', grouplist);
var grp=$.parseJSON(grouplist);
consoloe.log(grp);
}
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
console.log('error', errorThrown);
}
});
console.log("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee");
}
users=userss;
console.log("gupppppppppppppppppp"+grupss);
console.log('success', users);
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
console.log('error', errorThrown);
}
});`
输出
groupnameeeee ["user00", "super user", "supreme user"] myjs.js:35 geeeeeeeeeeeeeeeeeeeeeeeeee ["user00", "super user", "supreme user"] myjs.js:48 geeeeeeeeeeeeeeeeeeeeeeeeee user00 myjs.js:49 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee myjs.js:80 geeeeeeeeeeeeeeeeeeeeeeeeee ["user00", "super user", "supreme user"] myjs.js:48 geeeeeeeeeeeeeeeeeeeeeeeeee super user myjs.js:49 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee myjs.js:80 geeeeeeeeeeeeeeeeeeeeeeeeee ["user00", "super user", "supreme user"] myjs.js:48 geeeeeeeeeeeeeeeeeeeeeeeeee supreme user myjs.js:49 eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee myjs.js:80 guppppppppppppppppppuser00,super user,supreme user myjs.js:84 success [Object, Object, Object] myjs.js:85 55555555555555555555["sample_vh.com"] myjs.js:55 ccccccccccccccccccccccsupreme user myjs.js:60 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["sample_vh.com"] myjs.js:62 rrrrrrrrrrrrrrrsample_vh.com myjs.js:67 ooooooooooooooooooooooooooo Object {supreme user: Array[1]} myjs.js:69 Uncaught SyntaxError: Unexpected token o myjs.js:70 55555555555555555555["veera_tls.com","v_v.com","sample_vh.com"] myjs.js:55 ccccccccccccccccccccccsupreme user myjs.js:60 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa["veera_tls.com","v_v.com","sample_vh.com"]
myjs.js:62 rrrrrrrrrrrrrrrveera_tls.com myjs.js:67 rrrrrrrrrrrrrrrv_v.com myjs.js:67 rrrrrrrrrrrrrrrrsample_vh.com myjs.js:67 ooooooooooooooooooooooooooo 对象{supreme user:Array [3]} myjs.js:69 未捕获的SyntaxError:意外的令牌o myjs.js:70 55555555555555555555 []
答案 0 :(得分:0)
我无法理解您的代码。
只需创建数组并将该数组传递给JSON.stringify
即可JSONify。
var arr = [1,2,3,4,5];
JSON.stringify(arr); // "[1,2,3,4,5]"
答案 1 :(得分:0)
$.ajax({
url: 'get_group_list',
type: 'POST',
async:false,
dataType:"json",
success: function (data) {
groupnamess=data;
for(var i in groupnamess)
{
grup[i]=groupnamess[i];
userss.push({
label:groupnamess[i],
value:groupnamess[i]
});
grupss.push(groupnamess[i]);
}
for(var i in grupss)
{
$.ajax({
url: 'get_user_list',
type: 'POST',
async:false,
dataType:"json",
data:{'name':groupnamess[i]},
success: function (data) {
if(data!='[]')
{
var group=groupnamess[i];
grouplist[group]=new Array();
var usrs=data;
for(var j in usrs)
{
grouplist[group].push(usrs[j]);
}
}
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
console.log('error', errorThrown);
}
});
}
users=userss;
console.log('successddddddd', users);
},
error:function(XMLHttpRequest, textStatus, errorThrown)
{
console.log('error', errorThrown);
}
});