简体   繁体   中英

Codeigniter weird ajax result in controller

I've started a codeigniter project and now ai have to do an ajax call to a special controller.

It goes something like this: - I have two select fields: 1 for selecting county's and the other one must populate with the city's in the selected county.

The problem is I get a really weird result when I send the selected county id to the ajax controller.

If I put something like this in the controller : echo "a $county_id" in the controller it will give me the response : a [selected county_id], but if I only echo out the county id it shows some json empty response, and I don't know what's wrong, below is the code:

  1. the ajax(jquery) call.

      $(document).ready(function(){ $('#judet').change(function(){ var county = $( "select option:selected" ).val(); $.ajax({ type:'POST', /*dataType: "json",*/ data:{cou_county:county}, url:'<?php echo base_url("ajax_controller/") ?>', success:function(){ console.log('id_judet:' + county); }, error:function(mesaj){ console.log("there's an error"); } }); }); }); 
  2. The codeigniter ajax_controller Controller:

      public function index() { header('content-type text/html charset=utf-8'); $cou_county = $this->input->post('cou_county'); $decodedCounty = $cou_county; echo "$decodedCounty"; } 

    The county and city selectors are two simple select inputs with id's I'll post some pictures if you think i haven't explained it well eneugh.

EDIT: the ajax call does access the controller, where i actually echo that id, but in the response i get some weird json thingy instead of the number, like in the picture below:

the response

Thanks for taking the time to read this, and thanks in advance to those of you who take the time to help a brother out.

You can't use PHP code in javascript:

<?php echo base_url("ajax_controller/") ?>

Instead, you should create a js variable like this:

base_url = '<?php echo base_url("ajax_controller/") ?>';

Then use this variable with your function.

In order to use base_url() you have to load url helper in controller like this..

$this->load->helper('url');

Or load in application/config/autoload.php .

Then in your ajax edit url to

url:'<?php echo base_url("ajax_controller/index");?>',

In controller:

public function index()
    {       
        $cou_county = $this->input->post('cou_county');
        echo $cou_county;
    }

In ajax:

$.ajax({
                type:'POST',
                /*dataType: "json",*/
                data:{cou_county:county},
                url:'<?php echo base_url("ajax_controller/index");?>',
                success:function(data){
                    console.log(data); //see your console
                },
                error:function(data){
                    console.log("there's an error");
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM