我要创建一个名为“ file.json.php”的文件
其中包含来自PHP的var,例如$ _POST ['foo']。
此文件将根据ajax调用传递的post var的值生成不同的结果。
要为调用file.php.json创建Ajax,必须进行哪些配置?如何将数组作为参数传递给json文件?
jqxhr = $.ajax('../files/data/file.json').
这是Json文件。
{
"data": [{
"work": "Symfony No. 3 in D minor",
"id": "1",
"composer": "Anton Bruckner"
}, {
"work": "Violin Concerto in E minor",
"id": "2",
"composer": "Mendelssohn Felix"
}, {
"work": "Symfony No.1 in C major",
"id": "3",
"composer": "Beethoven, Ludwig van"
}, {
"work": "Solution for dynamic headers in datatables",
"id": "4",
"composer": "Fasani, Guza"
}],
"columns": [
{
"data": "work",
"name": "Work"
},
{
"data": "id",
"name": "Product ID"
},
{
"data": "composer",
"name": "Composer"
}
]
}
答案 0 :(得分:0)
最后,我制作了一个以php扩展名file.json.php结尾的文件,并将其像php普通文件一样进行管理,但使用json_encode()函数添加了回显。
这是我制作文件的方式的一部分。
//only making to arrays an array (in this case for datatables dynamic columns on the fly)
$columns[] = array("data" => "id", "name"=>"Id");
$columns[] = array("data" => "number", "name"=>"numeroItem");
$columns[] = array("data" => "item", "name"=>"Item");
$a = array();
$a['id'] = $row[$k]['id'];
$a['number'] = $row[$k]['number'];
$a['item']= $row[$k]['Item'];
$data[] = $a;
$json_for_datatable = array("columns" => $columns, "data" => $data);
echo json_encode($json_for_datatable);
出于配置或考虑因素的考虑,必须使用JSON.parse()函数接收de responseText,“这很重要!
data = JSON.parse(jqxhr.responseText);
考虑在ajax调用中
var data,
tableName = '#exampleTablaDinamica'+$(this).attr("data-id"),
columns, str,
jqxhr = $.ajax('../files/data/data.json.php?idPartida='+$(this).attr("data-idpartida")+'&meses='+ selected +'&idPresupuesto='+$('#idPresupuesto').val())
.done(function () {
data = JSON.parse(jqxhr.responseText);
// Iterate each column and print table headers for Datatables
$(tableName + " thead").html("<tr></tr>");
$.each(data.columns, function (k, colObj) {
str = '<th>' + colObj.name + '</th>';
$(str).appendTo(tableName+'>thead>tr');
});
// Add some Render transformations to Columns
// Not a good practice to add any of this in API/ Json side
data.columns[0].render = function (data, type, row) {
return data;
}
data.columns[1].render = function (data, type, row) {
return data;
}
data.columns[2].render = function (data, type, row) {
return "<a href='a.php'>" + data + "</a>";
}
// Debug?
console.log(data.columns[0]);
if ( $.fn.DataTable.isDataTable(tableName) ) {
$(tableName).DataTable().destroy();
}
table = $(tableName).dataTable({
"data": data.data,
"columns": data.columns,
"responsive":true,
"fnInitComplete": function () {
// Event handler to be fired when rendering is complete (Turn off Loading gif for example)
console.log('Datatable rendering complete');
}
});
})
.fail(function (jqXHR, exception) {
var msg = '';
if (jqXHR.status === 0) {
msg = 'Not connect.\n Verify Network.';
} else if (jqXHR.status == 404) {
msg = 'Requested page not found. [404]';
} else if (jqXHR.status == 500) {
msg = 'Internal Server Error [500].';
} else if (exception === 'parsererror') {
msg = 'Requested JSON parse failed.';
} else if (exception === 'timeout') {
msg = 'Time out error.';
} else if (exception === 'abort') {
msg = 'Ajax request aborted.';
} else {
msg = 'Uncaught Error.\n' + jqXHR.responseText;
}
console.log(msg);
});
最后这是生成的JSON内容。
{
"columns": [{
"data": "id",
"name": "Id"
}, {
"data": "number",
"name": "number"
}, {
"data": "item",
"name": "Item"
}, {
"data": "10 2018",
"name": "10 2018"
}, {
"data": "11 2018",
"name": "11 2018"
}, {
"data": "total",
"name": "total"
}],
"data": [{
"id": "1",
"number": "1.1",
"item": "loremipsum loremipsum loremipsum loremipsum ",
"10 2018": "0",
"11 2018": "0",
"total": "20.741"
}]
}