簡體   English   中英

從codeigniter函數檢索並顯示json數據

[英]Retrieve and display json data from codeigniter function

如何使用ajax(json)和codeigniter或php顯示數據庫(SQL)中的數據? 我試圖這樣做,但結果是“未定義”,這是我的代碼:

jQuery代碼:

$('[id^="menuitem_"]').on('click',function(){
var menuID = $(this).attr('id').split("_")[1];
        $.ajax({
            url:"<?php echo base_url();?>Vendors_search/Get_Business_By_Type",
            method:"POST",
            dataType: "text",
            data: {menuID:menuID},
            success:function(resp){

              var obj = $.parseJSON(resp);
              var values = Object.values(obj); 
              $.each(obj,function(key,val){

              $('.business_id').append('<strong>' + val.business_name + '</strong>');
            });
    }

        });

Codeigniter代碼:

function Get_Business_By_Type(){
    $obj = $this->input->post('menuID');     
    if($this->Search_obj_model->getBusinessType($obj)){
    $bus_type = $this->Search_obj_model->getBusinessType($obj);  
    $result['business_details'] = $this->Search_obj_model->getBusinessType($obj);
        }
        else{
            $result['message'] = 'Oooops! We could not find your request. Try again later';
        }
    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($result));
    $string = $this->output->get_output();
    echo $string;
    exit();
 }

執行以下操作以顯示JSON數據

在Ajax調用中將dataType更改為json

無需添加output->set_content_type('application/json')

$result['business_details'] = $this->Search_obj_model->getBusinessType($obj);
    }
    else{
        $result['message'] = 'Oooops! We could not find your request. Try again later';
    }
echo $result;

從Ajax獲取數據

$.each(resp, function (key, data) {

  $.each(data, function (index, data) {
      data.business_name // Your Value
   });
 });

暫無
暫無

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

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