Javascript 使用来自 Ajax 响应的 JSON 数据数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28985363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
working with JSON data array from Ajax response?
提问by Goodbytes
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.
是否可以在 PHP 中处理来自 AJAX 请求的响应?我不是真正的 JS 开发人员,所以我正在用这个来调查我的头发。
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.
我需要能够将其输出给用户,以便它是可读的。我找到的很多指南都描述了替换数据,但就目前而言,在选择 child_id 之前没有数据......然后我希望它以可读的方式显示上述数据。
I have no idea how to start working with the data in my view file(php).
我不知道如何开始处理我的视图文件(php)中的数据。
Thanks
谢谢
[EDIT]updated with working code:
[编辑]更新了工作代码:
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>£" + payment.amount + " - " + date + " (" + payment.club + ")</li>";
}
output += "</ul>";
$('.history-section').html(output);
},
dataType: "html"
});
});
回答by Murtaza Khursheed Hussain
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>";
回答by Manoj Kumar
回答by Ronak Patel
Use $.parseJSON() For Convert Json Format Data To Array
使用 $.parseJSON() 将 Json 格式数据转换为数组
Right code at sucess of ajax.. Like,
ajax成功的正确代码..喜欢,
var data = $.parseJSON(html);
var data = $.parseJSON(html);
data in you get array format of responce
数据在你得到响应的数组格式
回答by paddyfields
You need to use json_encode() in your php file to send the data back as an array
您需要在 php 文件中使用 json_encode() 将数据作为数组发回
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;
然后你可以像这样在js文件中单独访问数据;
success: function(html) {
alert(html.data1);
alert(html.data2);
},
You also need to change the dataType to 'json'
您还需要将 dataType 更改为 'json'
回答by Kamal Kumar
$('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;
}
});
You Can map your json something like this
你可以像这样映射你的json

