簡體   English   中英

使用jQuery AJAX請求從數據庫到控制器從數據庫顯示數據到視圖CodeIgniter

[英]Displaying data from database from Controller to View using jQuery AJAX request CodeIgniter

我在使用jQuery AJAX作為json數據類型從數據庫中獲取數據時遇到了問題,我已經在jQuery中閱讀並嘗試了JSON.parse,但是沒有用。 我在console.log(data)返回的只是object ,而數據庫中的數據則是object ,但是我不知道如何在視圖上顯示它。 有任何建議請。

這是我的控制器。

  function fetch(){
                $data_fetch     = array(
                    'id'        => $this->input->post('txt_id')
                    );
                $data['records'] = $this->form_model->fetch_model($data_fetch);
        $results    = json_encode($data);

            $this->output->set_content_type('application/json');
            $this->output->set_output($results);
        }

這是我的jQuery AJAX

$.ajax({
            type    : 'POST',
            url     :  base_url + 'index.php/form_controller/fetch',
            data    : {txt_id : id },
            dataType : 'JSON',
            success : function(data){
                // var re = JSON.parse(data);
                // var re = $.parseJSON(data);
                // var data = JSON.parse(data);
                alert(data.name);
                console.log(data);

            },
            error   : function(data){
                console.log('erronr in fetch');
            }
        });

data.name在警報中顯示為undefined 任何幫助將不勝感激。

您不需要使用JSON.parse變量數據已經是JSON,因此只需像使用其他任何JSON一樣使用它即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM