2013-01-07 62 views
0

我想創建一個工具來打印包含ajax,json和CodeIgniter組合的數據。但是當數據返回時,錯誤發生在ajax中。我製作了ajax和控制器。使用json和codeigniter返回數組

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 : [email protected]; Bank yyy Cabang www No. TTT</b>'; 
    } 
    echo json_encode(array('print'=>$print)); 
} 

錯誤恢復數據時到AJAX發生。請幫忙。謝謝

+0

的就是你這個URL請求中找到確切的錯誤?我不認爲你的網址到達了正確的網址。 –

+0

你在成功功能中獲得了什麼? 'value.print.head;'這是否讓打印arr中的頭部? – Jai

+0

你可以發佈你的示例json結構嗎? – Jai

回答

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/

+0

我認爲沒有問題,我的網址,但不能從返回json調用數據到ajax。 – ramadani

+0

@ user1878292請檢查更新後的答案,並通過jsfiddle,希望這會對你有所幫助 –

+0

你的jsfiddle中沒有代碼 – ramadani

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而不是阿賈克斯