在ajax中的div中追加返回值

时间:2017-07-24 12:29:03

标签: javascript jquery ajax codeigniter

  

我想在ajax中的div中追加返回数组值。   我的返回数组值是

my-project-0.0.1.jar
      |___ myconfiguration1.conf
      |___ myconfiguration2.conf
  

我的脚本是

  Array
  (
  [0] => Array
    (
        [m_id] => 35
        [event_id] => 421
        [activity_id] => 0
        [from] => dipti
        [to] => 1,2
        [cc] => 1
        [bcc] => 2
        [subject] => test
        [content] => dsfssdfsdfsdf
        [reply_before] => 0000-00-00
        [inreplyof_mid] => 0
        [is_important] => 
        [is_deleted] => 
    )

  )
  

当我追加它显示“未定义”消息时,我如何在这个div中追加我的数组值。

2 个答案:

答案 0 :(得分:1)

最好的方法是在PHP中你需要返回

Json_encode($array);

在Javascript中:

var result=$.parseJSON(data);
   console.log(result.from);

您的Jquery将如下所示:

$.ajax({
     type: "POST",
     async: false,
     dataType: "json",
     url: "<?php echo site_url(); ?>admin/get_mail_contents",
     data: datastring,
     cache: false,
     success: function (data) {
     var result=$.parseJSON(data);
     console.log(result);
     $("#content").append('<div class="mail-toolbar clearfix"> <div class="float-left"><h4 class="font-primary">This is a demo email title</h4></div><div class="float-right"><a href="#" class="btn btn-primary" title="Reply"> Reply <i class="glyph-icon icon-mail-reply"></i></a><a href="#" class="btn btn-default" title="Print"><i class="glyph-icon icon-print"></i></a><a href="#" class="btn btn-danger mrg10L" title="Delete"> <i class="glyph-icon icon-trash-o"></i></a> </div></div><div class="divider"></div><div class="pad5A clearfix mrg10B"><div class="float-left"><b>"' + result.from  + '"</b> (john.doe@example.com) <i>to</i> <b>me</b></div> <div class="float-right opacity-80"><i class="glyph-icon icon-clock-o mrg5R"></i> 17:55, 15.10.2014</div></div><div class="email-body"><p>"' + result.content + '"</p></div><div class="button-pane"><a href="#" class="btn btn-blue-alt" title="Reply"><i class="glyph-icon icon-mail-reply"></i> Reply </a><a href="#" class="btn btn-default" title="Reply">Forward<i class="glyph-icon icon-mail-forward"></i> </a> </div>');  
     }
 });

答案 1 :(得分:0)

将数据类型设为json并以json格式接收它并尝试。