In my controller I have a method that gets triggered by an AJAX call. I have a list of strings that I want to return
List<string> Usernames = new List<string>();
Then when data is loaded into Usernames I convert it into JSON
var JsonResults = Json(Usernames);
finally I return that JSON as below
return Json(new { success = true, resultsList =JsonResults });
In JavaScript, How can I loop through that array resultsList? Here is what the JS code looks like -
$.ajax({
url: "@Url.Action("StartNewChat")",
data: { SearchedText: searchedText },
type: "GET",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result.success == true) {
// READ THROUGH result.resultsList
}
}
});
I tried JSON.parse() and result.resultsList[0] and converting result.resultsList into string and back to JSON it didn't work.
Edit:
When I do a console.log(result.resultsList) here is what I get which is pretty strange
{"contentType":null,"serializerSettings":null,"statusCode":null,"value":["a","aa","aaa"]}
the last array is the result from Username array in c#