如何在ajax返回时解析字符串的JSON列表? [英] How to parse JSON list of string on ajax return?

查看:94
本文介绍了如何在ajax返回时解析字符串的JSON列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ajax调用,它返回一个序列化的字符串列表。这很好,我可以恢复数据。但是,我只是尝试对列表中的每个项目执行警报。但是,我只是继续从列表中找回单个字符。例如,如果它返回一个列表,其中包含一个名为Hello的项目。它会提醒H,E,L等。有人可以帮助我改变它,以便提醒完整的字符串吗?



收到的回复是非常的类似于上面的文字。如果c#变量userList返回一个只有Andrew的字符串列表。 JQuery会提醒A,N,D等。如果不清楚,请告诉我。



谢谢



C#

  [HttpPost] 
公共字符串GetUserList(string Role){
List< string> UserList = new List< string>();
UserList = Roles.GetUsersInRole(Role).ToList();
返回JsonConvert.SerializeObject(UserList);
}

JQuery

  $('#allRolesDD')。change(function(){
$ .ajax({
method:POST,
url:。 / GetUserList,
data:{Role:$(this).val()}
})
.done(function(data){
$('。roleDD' ).empty();
for(i = 0; i< data.length; i ++){
alert(data [i]);
}
console.log (通过4);
})
.fail(function(){
console.log(失败4);
})
}) ;


解决方案

你可以改变c#代码和jquery,如下所示: / p>

C#

  [HttpPost] 
public JsonResult GetUserList( string Role){
List< string> UserList = new List< string>();
UserList = Roles.GetUsersInRole(Role).ToList();
返回Json(UserList,JsonRequestBehavior.AllowGet);
}

JQuery

  $('#allRolesDD')。change(function(){
$ .ajax({
method:POST,
url:。 / GetUserList,
contentType:application / json; charset = utf-8,
dataType:json,
data:{Role:$(this).val()}
})
.done(函数(数据){
$('。roleDD')。empty();
for(i = 0; i< data.length ; i ++){
alert(data [i]);
}
console.log(Passed 4);
})
.fail(function( ){
console.log(失败4);
})
});


I have a simple ajax call which returns a serialised list of strings. This is great and I can get the data back. However I'm simply trying to perform an alert on each item in the list. However, I just keep getting back single characters from the list instead. For example if it returned a list with one item in it called "Hello". It would alert "H", "E", "L" etc. Can someone help me change this so it alerts the full string?

The response received back is very similar to the text above. If the c# variable userList returns a list of strings with just "Andrew" in it. The JQuery will alert "A", "N", "D" etc. If that isn't clear, just let me know.

Thanks

C#

[HttpPost]
        public string GetUserList(string Role) {
            List<string> UserList = new List<string>();
            UserList = Roles.GetUsersInRole(Role).ToList();
            return JsonConvert.SerializeObject(UserList);
        }

JQuery

   $('#allRolesDD').change(function () {
        $.ajax({
            method: "POST",
            url: "./GetUserList",
            data: { Role: $(this).val() }
        })
        .done(function (data) {
            $('.roleDD').empty();
            for (i = 0; i < data.length; i++) {
                alert(data[i]);
            }
            console.log("Passed 4");
        })
        .fail(function () {
            console.log("Failed 4");
        })
    });

解决方案

you can change c# code and jquery like below:

C#

[HttpPost]
    public JsonResult GetUserList(string Role) {
        List<string> UserList = new List<string>();
        UserList = Roles.GetUsersInRole(Role).ToList();
        return Json(UserList, JsonRequestBehavior.AllowGet);
    }

JQuery

  $('#allRolesDD').change(function () {
$.ajax({
    method: "POST",
    url: "./GetUserList",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: { Role: $(this).val() }
})
.done(function (data) {
    $('.roleDD').empty();
    for (i = 0; i < data.length; i++) {
        alert(data[i]);
    }
    console.log("Passed 4");
})
.fail(function () {
    console.log("Failed 4");
})
});

这篇关于如何在ajax返回时解析字符串的JSON列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆