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

编程入门 行业动态 更新时间:2024-10-28 21:19:22
如何在ajax返回时解析字符串的JSON列表?(How to parse JSON list of string on ajax return?)

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

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

谢谢

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"); }) });

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"); }) });

最满意答案

您可以像下面那样更改c#代码和jquery:

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"); }) });

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"); }) });

更多推荐

本文发布于:2023-08-04 16:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   列表   如何在   ajax   return

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!