簡體   English   中英

codeigniter 與 ajax json_encode 不工作

[英]codeigniter with ajax json_encode not working

我需要幫助....問題是代碼什么都不做,我不知道是什么問題

這是我在視圖中的代碼

  <div id="ysfhkm_slc_country">
     <select name="ysfhkm_slc_country" id="">
       <option value="ts1" selected>Test</option>
     </select >
  </div>

 <div id="ysfhkm_slc_negh">
     <select name="ysfhkm_slc_negh" id="">
       <option value="ts1" selected>Test</option>
     </select >
  </div>

 <div id="ysfhkm_slc_city">
     <select name="ysfhkm_slc_city" id="">
       <option value="ts1" selected>Test</option>
     </select >
  </div>

這是我在視圖中的代碼“Js”

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script type="application/javascript">

    /* hkm  */
    $(document).ready(function(){   
       $("#ysfhkm_slc_negh select").change(function () {     
            var state_value = $(this).val(); 
            var country_valueee = $("#ysfhkm_slc_country select").val();
            $.ajax({
                url:'https://www*mysite*com/Search_controller/GetCitiesOfState',
                method: 'POST',
                data: {state_val: state_value, country_id: country_valueee},
                dataType: 'json',
                success: function(data){
                    $('#ysfhkm_slc_city select').html(data);

                }
            });
            return false;
        });

        // get states of country
        $("#ysfhkm_slc_country select").change(function () { 
            var country_value = $(this).val(); 
            $.ajax({
                url:'https://www*mysite*com/Search_controller/GetStatesOfCountry',
                method: 'post',
                data: {country_val: country_value },
                dataType: 'json',
                success: function(data){
                    $('#ysfhkm_slc_negh select').html(data);
                    console.log('done');  
                },
                error: function (reponse) {
                    console.log('Problem with ajax');
                }

            });
            $.ajax({
                url:'https://www*mysite*com/Search_controller/GetCitiesOfState',
                method: 'POST',
                data: {state_val: 'ts1', country_id: country_value},
                dataType: 'json',
                success: function(data){
                    $('#ysfhkm_slc_city select').html(data);
                }
            });
            return false;
        });


    });


/* end  hkm  */
</script>

這是我的代碼 Search_controller

問題是代碼什么都不做,我不知道是什么問題

我制作了模型代碼來測試firt ajax +控制器但不工作

<?php 

    class Search_controller extends CI_Controller{

        public function index(){


        }



        public function GetCitiesOfState(){
          /* comments
            if($this->input->post('state_val') && $this->input->post('country_id')){
                $postData = $this->input->post('state_val');
                $country_id = $this->input->post('country_id');

                $this->load->model('locationhkm_model');
                $data = $this->locationhkm_model->getUserCitiesOfState($postData,$country_id);
                echo json_encode($data);
            }
            */
              $postData = $this->input->post('state_val');
              $country_id = $this->input->post('country_id');
              $data = '<option value="222">citiessssssss</option>';
              echo json_encode($data);

        }

        public function GetStatesOfCountry(){
          /* comments
            if($this->input->post('country_val')){
                $postData = $this->input->post('country_val');
                $this->load->model('locationhkm_model');
                $data = $this->locationhkm_model->getUserStatesOfCounrty($postData);
                echo json_encode($data);
            }
            */
             $postData = $this->input->post('country_val');
              $data = '<option value="444" >statessssssss</option>';
              echo json_encode($data);
        }


    }


    ?>

問題出在您的 controller GetStatesOfCountry function 返回值中。 改成這樣

public function GetStatesOfCountry(){
              $postData = $this->input->post('country_val');
              //your logic
              $data['value'] ="444"
              $data['text'] ="statessssssss"
              echo json_encode($data);
        }

將您的 URL 更改為

url:"<?php echo base_url()?>index.php/Search_controller/testing_controller",

並且在你的 ajax 里面成功 function,

 success: function(data){
     var return = $.parseJSON(data);
     optionText = return.value; 
     optionValue = return.text; 

     $('#ysfhkm_slc_city').append(`<option value="${optionValue}">
        ${optionText} 
     </option>`); 
 }

到 append 到 select 的新選項標簽,您可以使用

var select = document.getElementById('ysfhkm_slc_city');
var opt = document.createElement('option');
opt.value = return.value;
opt.innerHTML = return.text;
select.appendChild(opt);

暫無
暫無

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

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