And*_*urn 6 javascript c# ajax jquery json
我有一个简单的 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);
}
Run Code Online (Sandbox Code Playgroud)
查询
$('#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");
})
});
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以像下面这样更改 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);
}
Run Code Online (Sandbox Code Playgroud)
查询
$('#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");
})
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13731 次 |
| 最近记录: |