我想将一个JSON从php传递给js。
这是我的php文件:
public function upload() {
$record = 0;
if (!empty($_FILES)) {
$config['upload_path'] = "./assets/uploads";
$config['allowed_types'] = 'csv';
$this->load->library('upload', $config);
if (!$this->upload->do_upload("file")) {
echo "File cannot be uploaded";
} else {
$upload_data = $this->upload->data();
$csv = $upload_data['full_path'];
$name_csv = $upload_data['file_name'];
$tryOne = array();
if (file_exists($csv)) {
$file = fopen($csv, 'r'); // r flag is for readonly mode
while (( $line = fgetcsv($file) ) !== false) { // if line exists
$tryOne[] = $line; // add to array
}
fclose($file);
}
echo json_encode(array(
"nama_csv" => $name_csv,
"path" => $csv,
"isi" => $tryOne
));
}
} elseif ($this->input->post('file_to_remove')) {
$file_to_remove = $this->input->post('file_to_remove');
unlink("./assets/uploads/" . $file_to_remove);
} else {
$this->listFiles();
}
}
请参阅mv json。我调试它:print_r($tryOne);
Array
(
[0] => Array
(
[0] => NO
[1] => COLUMN1
[2] => COLUMN2
[3] => COLUMN3
[4] => COLUMN4
[5] => COLUMN5
[6] => COLUMN6
[7] => COLUMN7
[8] => COLUMN8
[9] => COULMN9
[10] => COLUMN10
[11] => COLUMN11
[12] => COLUMN12
[13] => COLUMN13
)
[1] => Array
(
[0] => 1
[1] => NYK FUJI
[2] => AJU150708
[3] =>
[4] => 6C7132
[5] => 977NEF
[6] => JKT-P.T.IRON WORKS
[7] => 977NEF
[8] => KCH8ATDM
[9] => 17.9
[10] => 1690
[11] => 2150
[12] => 6C7132-1690
[13] => 175
)
[2] => Array
(
[0] => 2
[1] => NYK FUJI
[2] => AJU150708
[3] =>
[4] => 6C7132
[5] => 977NEF
[6] => JKT-P.T.IRON WORKS
[7] => 977NEF
[8] => KCH8ATDM
[9] => 17.9
[10] => 1700
[11] => 2138
[12] => 6C7132-1700
[13] => 176
)
)
一切都很好看。但是在ajax的成功中:
success: function (response) {
moment.locale("id");
for (i = 0; i <= response.isi.length; i++) {
for (j = 0; j <= response.isi[i].length; j++) {
$('#table-review').find('tbody').append("<tr>" +
"<td>" + response.isi[i][j] + "</td>" +
"</tr>");
}
}
$("#btnSave").attr("onclick", "save('" + response.path + "', '" + response.nama_csv + "')");
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Review Data Hasil Upload Dari CSV Pusat'); // Set Title to Bootstrap modal title
reload_table();
listFilesOnServer();
},
给了我: TypeError:response.isi [i]未定义
我想将这个数组显示在模态引导程序的表中,所以我再次调试它以查看response.isi是否存在:console.log(response.isi)
[["NO", "COLUMN1", "COLUMN2", 11 more...]
我有点困惑,为什么在php数组和js数组看起来不同,
在php中分隔成元素,但在js中用逗号分隔。
更新
根据以下建议,我得到了这样的回调:
Object { 0="NO", 1="COLUMN1", 2="COLUMN2", more...}
Object { 0="1", 1="NYK FUJI", 2="AJU150708 ", more...}
Object { 0="2", 1="NYK FUJI", 2="AJU150708 ", more...}
所以,我用这样的循环:
index = 1;
$.each(response.isi, function (i, item) {
$('#table-review').find('tbody').append("<tr>" +
"<td>" + index + "</td>" +
"<td>" + item.i + "</td>" +
"</tr>");
index++;
console.log(item);
});
它给了我:
+---+-----------+
| 1 | undefined |
| 2 | undefined |
| 3 | undefined |
+---+-----------+
请告知。
答案 0 :(得分:0)
就像这样编码
class ExcelClass
{
[DllImport("user32.dll")]
static extern int GetWindowThreadProcessId(int hWnd, out int lpdwProcessId);
Process GetId(Excel.Application excelApp)
{
int id;
GetWindowThreadProcessId(excelApp.Hwnd, out id);
return Process.GetProcessById(id);
}
}
在ajax中你必须解析它。因为你似乎在使用jquery,所以你可以这样做
echo json_encode(array(
"nama_csv" => $name_csv,
"path" => $csv,
"isi" => $tryOne
), JSON_FORCE_OBJECT);
然后你得到数组。像data = jQuery.parseJSON(response);
或data.isi
一样使用它。根据您的数组类型,不确定哪一个会起作用。
答案 1 :(得分:0)
更新后的错误:
您正在循环中访问错误的索引。请参阅更正后的代码:
var index = 1;
$.each(response.isi, function (i, item) {
$('#table-review').find('tbody').append("<tr>" +
"<td>" + index + "</td>" +
//"<td>" + item.i + "</td>" + // wrong: i doesn't exist as a property of item
"<td>" + item[i] + "</td>" + // right: item array at index i will work
"</tr>");
index++;
console.log(item);
});