dongwo5110 2015-06-16 12:24
浏览 35

使用ajax显示来自json数组的数据

I have the following code on index page the script contains part of the code that will call the data from test page

<div class="leftbox" id="proddisplay">

</div>

var onSubmit = function(e) {
  var txtbox = $('#txt').val();
  var hiddenTxt = $('#hidden').val();
  $.ajax({
    type: 'post',
    url: 'test.php',
    data: {
      txt: txtbox,
      hidden: hiddenTxt
    },
    cache: false,
    success: function(returndata) {
      $('#proddisplay').html(returndata);
      console.log(returndata);
    },
    error: function() {
      console.error('Failed to process ajax !');
    }
  });
};

From test.php i am getting json array that looks like this

[1,2,"text","text2"]

I want to display the json array data in a tabular form and display it inside the div. the view of table should be something like this (it will have some css of its own)

static text: 1
static text: 2
static text: text
static text: text2

static text will be given by me and remains the same throughout however the values of array would change. can anyone tell how i can do so

individually i can display the data from json, but here i also need to put json data in a table and then put that table within a div

  • 写回答

1条回答 默认 最新

  • dongzhoulong1797 2015-06-16 12:31
    关注

    First of all you need to encode the array in php before sending to frontend, use json_encode() and then decode it in the success function using JSON.parse() . After just run a loop throught the array.

    var onSubmit = function(e) {
     var txtbox = $('#txt').val();
     var hiddenTxt = $('#hidden').val();
     $.ajax({
       type: 'post',
       url: 'test.php',
       data: {
         txt: txtbox,
         hidden: hiddenTxt
       },
       cache: false,
       success: function(returndata) {
         var returneddata = JSON.parse(returndata);
         var htmlData= '';
         for(var i=0; i<returneddata.length; i++){
            htmlData+= 'static text: '+returneddata[i]+' <br>';
         }
         $('#proddisplay').html(htmlData);
         console.log(returndata);
       },
       error: function() {
         console.error('Failed to process ajax !');
       }
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集