使用Ajax响应中的JSON数据数组?

时间:2015-03-11 11:24:32

标签: javascript php ajax json

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

我有点一起攻击这个:

      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; 

});

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

{"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才会有数据。然后我希望它以可读的方式显示上述数据。

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

由于

[EDIT]使用工作代码更新:

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"
  });
});

5 个答案:

答案 0 :(得分:5)

这样做。

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>";

答案 1 :(得分:2)

使用

dataType: 'json',

而不是

dataType: 'html',

然后使用each 从成功函数

中的响应中获取记录

答案 2 :(得分:2)

使用$ .parseJSON()将Json格式数据转换为数组

ajax成功的正确代码.. 像,

var data = $ .parseJSON(html);

您获得数据格式的响应

答案 3 :(得分:0)

你需要在php文件中使用json_encode()将数据作为数组发回

例如;

$myarray = array("data1"=>"value1","data2"=>"value2");

echo json_encode($myarray);

然后,您可以像这样在js文件中单独访问数据;

 success: function(html) { 
    alert(html.data1);
    alert(html.data2);
  },

您还需要将dataType更改为&#39; json&#39;

答案 4 :(得分:0)

$('input[name=\'product_attribute[' + attribute_row + '][name]\']').catcomplete({
    delay: 0,
    source: function(request, response) {
        $.ajax({
            url: 'index.php?route=catalog/attribute/autocomplete&token=<?php echo $token; ?>',
            type: 'POST',
            dataType: 'json',
            data: 'filter_name=' +  encodeURIComponent(request.term),
            success: function(data) {   
                response($.map(data, function(item) {
                    return {
                        category: item.attribute_group,
                        label: item.name,
                        value: item.attribute_id
                    }
                }));
            }
        });
    }, 
    select: function(event, ui) {
        $('input[name=\'product_attribute[' + attribute_row + '][name]\']').attr('value', ui.item.label);
        $('input[name=\'product_attribute[' + attribute_row + '][attribute_id]\']').attr('value', ui.item.value);

        return false;
    }
});

你可以映射你的json这样的东西