Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse JSON list of string on ajax return?

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");
        })
    });
like image 290
Andrew Kilburn Avatar asked Dec 14 '15 12:12

Andrew Kilburn


People also ask

How do I parse a string in JSON?

Example - Parsing JSONUse the JavaScript function JSON.parse() to convert text into a JavaScript object: const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); Make sure the text is in JSON format, or else you will get a syntax error.

How do I get AJAX response in JSON?

Approach: To solve this problem, we will first consider a JSON file named “capitals. json” and try to get this JSON data as a response using AJAX. Then we will create an HTML file “capitals. html” which contains a table which we will use to populate the data we are getting in response.

What does the JSON parse () method do when we initiate an AJAX request?

Description: Takes a well-formed JSON string and returns the resulting JavaScript value.

Does AJAX return JSON?

You couldn't directly return an array from AJAX, it must have converted in the valid format. In this case, you can either use XML or JSON format. In the tutorial demonstration, I will return an array of users from AJAX, while return converts the array into JSON format using the json_encode() function in the PHP.


1 Answers

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");
})
});
like image 183
Atikur Rahman Avatar answered Oct 23 '22 04:10

Atikur Rahman