我有一个简单的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"); }) });更多推荐
发布评论