使用Ajax响应中的JSON数据数组? [英] working with JSON data array from Ajax response?

查看:101
本文介绍了使用Ajax响应中的JSON数据数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用PHP中的AJAX请求响应?我不是一个真正的JS开发人员,所以我用这个来调查我的头发。

Is it possible to work with a response from AJAX request in PHP? I am not really a JS dev so i am polling my hair out with this one.

我有点一起攻击这个:

      var base_url = 'http://dev.local/westview/public';

  $('select.child_id').change(function() {

var child_id = $('#child_id');
var dataString = 'child_id=' + child_id;

$.ajax({
  type: "POST",
  url: base_url + "/finance/payment-history",
  data: dataString,
  dataType: 'html',
  success: function(html) { 
    alert(html);
  },

});
return false; 

});

该功能似乎工作正常,它会给我一个包含正确数据的提示。

The function appears to work ok, it gives me an alert with the correct data.

{"payments":[{"id":"19","child_id":"21","club":"Breakfast Club","term":"Half Term 3","amount":"15.00","pdate":"2015-02-25","notes":"","created_at":"2015-02-11 12:16:32","updated_at":"2015-02-11 12:16:32","starting_debt":"0","debt_start_date":"2015-01-05"},{"id":"20","child_id":"21","club":"After School Club","term":"Half Term 3","amount":"11.50","pdate":"2015-02-25","notes":"","created_at":"2015-02-11 12:16:49","updated_at":"2015-02-11 12:16:49","starting_debt":"0","debt_start_date":"2015-01-05"}]}

我需要能够将此输出给用户,以便它可读。我找到的很多指南都描述了替换数据,但是直到选择了child_id才会有数据。然后我希望它以可读的方式显示上述数据。

I need to be able output this to the user so that it is readable. A lot of guides I find describe replacing data but as it stands there is no data until a child_id is selected.. i then want it show the above data in a readable way.

我不知道如何开始使用我的视图文件(php)中的数据。

I have no idea how to start working with the data in my view file(php).

谢谢

用工作代码更新:

var base_url =' http://dev.local/westview/public ';

var base_url = 'http://dev.local/westview/public';

$('select.child_id').change(function() {

  var response = "";
  var child_id = $('#child_id').val();
  var dataString = 'child_id=' + child_id;

  $.ajax({
    type: "POST",
    url: base_url + "/finance/payment-history",
    data: dataString,
    success: function(response) { 

      var json_obj = $.parseJSON(response);

      var output = "<ul>";

      for (i=0; i < json_obj.payments.length; i++)
      {
        var payment = json_obj.payments[i];
        var date = moment(payment.pdate).format('Do MMM YYYY');
        output += "<li>&pound;" + payment.amount + " - " + date + " (" + payment.club + ")</li>";
      }

      output += "</ul>";

      $('.history-section').html(output);

    },
    dataType: "html"
  });
});


推荐答案

这样做。

var data = $.parseJSON("your_json");
var output= "<ul>";

for (i=0; i < data.payments.length; i++){
    output += "<li>" + data.payments[i].id + ", " + data.payments[i].child_id + "</li>";
}

output += "</ul>";

这篇关于使用Ajax响应中的JSON数据数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆