BugBusterX
BugBusterX

Reputation: 203

How can I "Parse" array returned by ajax?

I have 3 spans on my page that contain numbers. On click of a button I would like to retrieve an ajax response, and if the response is valid (it should be an array of 3 elements) I would like to update the numbers in these spans. Could you please recommend a solution via jQuery?

Thank You.

Upvotes: 1

Views: 784

Answers (2)

bearing09
bearing09

Reputation: 739

You can just implement this by usig jQuery.getJson(url,callback(data,textStatus)) .

for example :

$.getJSON(url, function(data,textStatus){ var spanValues = data.list; $('#span_Id').text(spanValues [i]); ...

});

Upvotes: 0

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

$.getJSON(url, function(resp)
{
  var list = resp.list;
  if(!list)
  {
    throw new Exception("list is not set");
  }
  for(var i = 0; i < list.length; i++)
  {
    $('#span' + (i + 1)).text(list[i]);
  }
});

if the spans have ids span1, span2, and span3. See $.getJSON for more information. Note that you can add error handling by using $.ajax instead.

Upvotes: 2

Related Questions