最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - working with JSON data array from Ajax response? - Stack Overflow

programmeradmin8浏览0评论

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.

I have sort of hacked this together:

      var base_url = '/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"}]}

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.

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

Thanks

[EDIT]updated with working code:

var base_url = '/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"
  });
});

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.

I have sort of hacked this together:

      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"}]}

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.

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

Thanks

[EDIT]updated with working code:

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"
  });
});
Share Improve this question edited Mar 11, 2015 at 14:23 Goodbytes asked Mar 11, 2015 at 11:24 GoodbytesGoodbytes 6942 gold badges10 silver badges28 bronze badges 4
  • what do you mean by readable format ? – Murtaza Khursheed Hussain Commented Mar 11, 2015 at 11:37
  • like maybe tabulate the array into rows or something. – Goodbytes Commented Mar 11, 2015 at 11:41
  • you are asking to parse data. use parseJSON (yourdata), this will convert you JSON string into Object, then you can Iterate it like payment[0].amount – Murtaza Khursheed Hussain Commented Mar 11, 2015 at 11:42
  • I have updated my first post. – Goodbytes Commented Mar 11, 2015 at 12:38
Add a ment  | 

5 Answers 5

Reset to default 6

Do like this.

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

use

dataType: 'json',

instead

dataType: 'html',

and then use each to fetch the record from response in success function

Use $.parseJSON() For Convert Json Format Data To Array

Right code at sucess of ajax.. Like,

var data = $.parseJSON(html);

data in you get array format of responce

You need to use json_encode() in your php file to send the data back as an array

For example;

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

echo json_encode($myarray);

You can then access the data separately in the js file like this;

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

You also need to change the dataType to 'json'

$('input[name=\'product_attribute[' + attribute_row + '][name]\']').catplete({
    delay: 0,
    source: function(request, response) {
        $.ajax({
            url: 'index.php?route=catalog/attribute/autoplete&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;
    }
});

You Can map your json something like this

发布评论

评论列表(0)

  1. 暂无评论