代码区项目交易流程

Codeigniter strange ajax drives a controller


I've started a codeigniter project and now a i 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:

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"); } }); }); });

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.

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

点击收藏

LAST typeof判断参数是否为undefined与全等判断法性能比较 Dew Drop January 4, 2019 (#2872) NEXT