使用AJAX获取JSON并使用响应数据

时间:2015-07-01 17:08:26

标签: javascript jquery ajax json

这是我的AJAX ...

$.ajax({
  url: "../getSynd.php",
  dataType:"json",
  success: function(jsVar){
    document.write(jsVar['sysinfo'][0]);
    syndStatus("canvas1", "first server name", "76","red");
    syndStatus("canvas2", "second server name", "1.2423","green");
  }
});

来自getSynd.php的我的JSON如下......

[{"sysinfo":"server1","result":"1.17805935"},{"sysinfo":"server2","result":"2069.59799893"}]

如何从jsVar中获取sysinfo / result?

编辑: 我想通了,谢谢大家!

$.ajax({
  url: "../getSynd.php",
  dataType:"json",
  success: function(jsVar){

    syndStatus("canvas1", jsVar[0]['sysinfo'], jsVar[0]['result']);
    syndStatus("canvas1", jsVar[1]['sysinfo'], jsVar[1]['result']);
  }
});

1 个答案:

答案 0 :(得分:2)

由于您已经在ajax中使用了dataType param,因此您将获得已经解析为javascript变量的响应。您只需要在成功回调ajax调用时捕获它,如下所示:

$.ajax({
    url: '...',
    dataType: 'json',
    success: function(jsVar) {
       //jsVar will be the javascript array
    }
});