未加星标

Codeigniter strange ajax drives a controller

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二03 | 时间 2019 | 作者 红领巾 ] 0人收藏点击收藏

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教程

分页:12
转载请注明
本文标题:Codeigniter strange ajax drives a controller
本站链接:https://www.codesec.net/view/628604.html


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(100)