5

Hello suppose I have the following ajax call:

jQuery.ajax({
    type: "GET",
    url: "https://myurl.com",
    success: function(data)
    {
        console.log(data);
    }
});

That results in the following json data (in my console)

{
  "meta": {
    "last_updated": "2017-07-06"
  },
  "results": [
    {
      "term": "DRUG INEFFECTIVE",
      "count": 1569
    },
    {
      "term": "NAUSEA",
      "count": 1374
    },
    {
      "term": "FATIGUE",
      "count": 1371
    }
  ]
}

How can I populate a div in my HTML page with this data in a format something like:

term: x
count: xx

term: x
count: xx

term: x
count: xx

I am not interested in the "meta" stuff only the "results"

Anyone know how to use a simple loop to display this data?

Thanks!

1
  • within success: function(data) { ... } "for loop" through data.results and then use jQuery to populate the targeted div. Commented Jul 17, 2017 at 0:39

3 Answers 3

3

You can loop through your data.results within your "success function", like this:

jQuery.ajax({
    type: "GET",
    url: "https://myurl.com",
    success: function(data)
    {
        console.log(data);

        jQuery.each(data.results, function(i, val) {
            // here you can do your magic
            $("#yourdivid").append(document.createTextNode(val.term));
            $("#yourdivid").append(document.createTextNode(val.count));
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you chris, can you help me out with what to put in the createTextNode?
awesome thank you very much this is exactly the answer I was looking for!
0

You want the success data which you receive in json format and for that, you first need to convert it in array to get the data

data = jQuery.parseJSON(data);

this will help you to receive the data in an array format and then you will do whatever you want to do with the data.

For more info look at these answers How to display JSON data with jQuery Ajax?

Comments

-1
StudentViewModel EmpInfo = new StudentViewModel();

        HttpClient client1 = new HttpClient();
        client1.BaseAddress = new Uri("http://localhost:2585/");

        client1.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var responsecountry = client1.GetAsync("api/Country/").Result;
        List<SelectListItem> country = new List<SelectListItem>();


        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://localhost:2585/");

        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        var response = client.GetAsync("api/StudentApi/" + id).Result;


        if (response.IsSuccessStatusCode)
        {
            EmpInfo = JsonConvert.DeserializeObject<StudentViewModel>(response.Content.ReadAsStringAsync().Result);
            if (responsecountry.IsSuccessStatusCode)
            {
                country = JsonConvert.DeserializeObject<List<SelectListItem>>(responsecountry.Content.ReadAsStringAsync().Result);
                EmpInfo.Country = country;
            }
            return PartialView(EmpInfo);
        }

1 Comment

this might be a valid answer, but clearly not for that question (which happens to already have an accepted answer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.