我想创建一个工具来组合ajax,json和CodeIgniter来打印数据。但是当返回数据时,错误发生在ajax中。我已经制作了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时发生错误。请帮忙。谢谢
答案 0 :(得分:0)
type: 'POST',
url: '<?php echo base_url('pembayaran/simpanitempembayaran'); ?>',
应该是:
type: 'POST',
url: '<?php echo base_url("pembayaran/simpanitempembayaran"); ?>',
UPDATE:这是显示json数据的示例。 http://jsfiddle.net/jogesh_pi/CRAcC/1/
答案 1 :(得分:0)
尝试将dataType: 'json'
包含在您的ajax调用中
dataType是您期望从服务器返回的数据类型。
$.ajax({
async: false,
type: 'POST',
dataType: 'json',
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;
}
});
或者您可以使用$.getJSON
代替ajax