Monday, May 20, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
67
rated 0 times [  69] [ 2]  / answers: 1 / hits: 16396  / 9 Years ago, mon, december 14, 2015, 12:00:00

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

More From » c#

 Answers
256

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

[#64070] Friday, December 11, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaredsages

Total Points: 273
Total Questions: 97
Total Answers: 105

Location: French Southern and Antarctic Lands
Member since Fri, Jan 6, 2023
1 Year ago
jaredsages questions
;