开发者

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.


$.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.


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]); ...

});

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜