我想创建一个工具来使用 ajax、json 和 CodeIgniter 的组合打印数据。但是当数据返回时,ajax出现错误。我做了ajax和控制器。
阿贾克斯
formPembayaran.on('submit', function(e){
e.preventDefault();
var serialized = $(this).serialize(), print = [];
$.ajax({
async: false,
type: 'POST',
url: '<?php echo base_url('pembayaran/simpanitempembayaran'); ?>',
data: serialized,
success: function(value){
print['head'] = value.print.head;
print['body'] = value.print.body;
print['foot'] = value.print.foot;
}
});
if (print.status === true) {
var popup = '<!DOC'+'TYPE HT'+'ML PUBLIC "-//W3C//DTD HT'+'ML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' +
'<ht'+'ml><he'+'ad><title>Cetak Materi Iklan Baris</title>' +
'<st'+'yle type="text/css">' +
'@media print {' +
'body {' +
'font-size: 11px;' +
'}' +
'button#printing {' +
'display: none;' +
'}' +
'}' +
'.center, h3 {' +
'text-align: center;' +
'}' +
'div#page_head {' +
'-webkit-column-count: 2; -webkit-column-gap: 1.5em; -moz-column-count: 2; -moz-column-gap: 1.5em; -o-column-count: 2; -o-column-gap: 1.5em; column-count: 2; column-gap: 1.5em;' +
'}' +
'div#page_body {' +
'-webkit-column-count: 2; -webkit-column-gap: 1.5em; -moz-column-count: 2; -moz-column-gap: 1.5em; -o-column-count: 2; -o-column-gap: 1.5em; column-count: 2; column-gap: 1.5em;' +
'}' +
'#page_body, #page_foot {' +
'margin-top: 10px;' +
'}' +
'</st'+'yle>' +
'</he'+'ad><bo'+'dy>' +
'<button id="printing" type="button" onClick="javascript:window.print();">Cetak Materi</button>' +
'<pre>' +
'<div id="content">' +
'<div id="page_head">' + print.head + '</div>' +
'----------------------------------------------------------------------------------------------------------' +
'<div id="page_body">' + print.body + '</div>' +
'<div id="page_foot">' + print.foot + '</div>' +
'</div>' +
'</pre>' +
'</bo'+'dy></ht'+'ml>';
var width = 860;
var height = 800;
var left = (screen.width/2)-(width/2);
var top = (screen.height/2)-(height/2);
var testpopup = window.open('','Printer','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+width+', height='+height+', top='+top+', left='+left);
testpopup.document.write(popup);
testpopup.document.close();
}
return false;
});
带json的控制器
function simpanItemPembayaran() {
$idMakanan = $this->input->post('id-makanan');
$namaMakanan = $this->input->post('nama-makanan');
$hargaMakanan = $this->input->post('harga-makanan');
$idMinuman = $this->input->post('id-minuman');
$hargaMinuman = $this->input->post('harga-minuman');
$tanggalItemPembayaran = $this->input->post('tanggal-pembayaran');
$tanggalPembayaran = date("Y-m-d", strtotime($tanggalItemPembayaran));
$idKasir = $this->input->post('id-kasir');
if (($idMakanan[0] == NULL) && ($idMinuman[0] == NULL)) {
$this->session->set_flashdata('flashError', '<b>WARNING!</b> Form pembayaran makanan dan minuman kosong');
} else {
foreach ($idMakanan as $keyMakanan => $makananId) {
if ($makananId != NULL) {
$simpanItemMakanan = $this->pembayaran_model->insertItemMakanan($makananId, $hargaMakanan[$keyMakanan], $tanggalPembayaran, $idKasir);
}
}
foreach ($idMinuman as $keyMinuman => $minumanId) {
if ($minumanId != NULL) {
$simpanItemMinuman = $this->pembayaran_model->insertItemMinuman($minumanId, $hargaMinuman[$keyMinuman], $tanggalPembayaran, $idKasir);
}
}
$this->session->set_flashdata('flashSuccess', 'Berhasil menyimpan item makanan atau minuman ke database');
$print['status'] = true;
$print['head'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['head'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['head'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['body'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['body'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['body'] = '<b>PT. BP. Kedaulatan Rakyat</b><br/>';
$print['foot'] = '<b>Website : www.kr.co.id; E-mail : alamatemail@mail.com; Bank yyy Cabang www No. TTT</b>';
}
echo json_encode(array('print'=>$print));
}
将数据恢复到 ajax 时发生错误。请帮忙。谢谢你