如何将响应数据设置为javascript代码

时间:2019-05-14 08:58:31

标签: javascript ajax

如何设置响应数据

$.ajax({
    type : "POST",
    async: true,
    url : "<?php echo base_url(); ?>" + "Graphs/get_graphs",
    dataType: 'json',
    data :{hotel_name_realm:$("#hotel_names").val()},
    success : function(res){
       obj = JSON.parse(res);
    console.log(obj.upload);
    console.log(obj.download);



series: [{
    name: 'Download',
    data: [
     (obj.Download) // I need set obj.download data here
    ]

}


}

Here is my console.log(obj) image

2 个答案:

答案 0 :(得分:0)

我喜欢这样,

$.ajax({
      method: "POST",
      url: "chat/searchuser",
      data: {name: $(this).val(), length: "10"}
      })
      .done(function (data) {
      $('#scroll_user').val('10');
      var html = '';
      $.each(JSON.parse(data).data, function (k, v) {
      html += '<div class="row sideBar-body getUserdata userchat" data-username="' + v.fname + '" data-id="<?php echo $key; ?>" onclick="showDiv()" id="' + v.fname + '"><div class="col-sm-3 col-xs-3 sideBar-avatar"><div class="avatar-icon"><img src="<?= base_url('assets/user2.jpg') ?>"></div></div><div class="col-sm-9 col-xs-9 sideBar-main"><div class="row"><div class="col-sm-12 col-xs-12 sideBar-name" data-username="' + v.fname + '"><span class="name-meta">' + v.fname + ' ' + v.lname + '(' + v.username + ')</span></div></div></div></div>'
      $('#usersindiv').html(html);
     });
    }).fail(function () {
     alert("failed");
     });

答案 1 :(得分:0)

您做对了,只是错过了右方括号。 JS也是区分大小写的,obj.Download不是obj.download

$.ajax({
    type : "POST",
    async: true,
    url : "<?php echo base_url(); ?>" + "Graphs/get_graphs",
    dataType: 'json',
    data :{hotel_name_realm:$("#hotel_names").val()},
    success : function(res){
       obj = JSON.parse(res);
       console.log(obj.upload);
       console.log(obj.download);

       series: [{
          name: 'Download',
          data: [
            (obj.Download) // shouldn' it be obj.download? (small D)
          ]
       ] // you are missing the right bracket here
    }
}