php Codeigniter如何接收控制器中的ajax post数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39461369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does Codeigniter receive the ajax post data in controller
提问by Leon
I'm trying to use CodeIgniter to develop the front-end client of my project.
我正在尝试使用 CodeIgniter 开发我项目的前端客户端。
But the ajax with CI make me confused.
但是带有 CI 的 ajax 使我感到困惑。
Here is my ajax:
这是我的ajax:
$.ajax({
url : "welcome/login"
type : "POST",
dataType : "json",
data : {"account" : account, "passwd" : passwd},
success : function(data) {
// do something
},
error : function(data) {
// do something
}
});
And the controller:
和控制器:
public function login() {
$data = $this->input->post();
// now I can get account and passwd by array index
$account = $data["account"];
$passwd = $data["passwd"];
}
Now I can get account and password by array index, but how can I convert received data to Object so I can get the property like: $data->account
现在我可以通过数组索引获取帐户和密码,但是如何将接收到的数据转换为 Object 以便我可以获得如下属性: $data->account
Thx!
谢谢!
回答by Abdullah Umar Babsel
Change your ajax this:
改变你的ajax这个:
$.ajax({
url : "<?php echo base_url(); ?>welcome/login"
type : "POST",
dataType : "json",
data : {"account" : account, "passwd" : passwd},
success : function(data) {
// do something
},
error : function(data) {
// do something
}
});
Change your controller this:
将您的控制器更改为:
public function login() {
//$data = $this->input->post();
// now I can get account and passwd by array index
$account = $this->input->post('account');
$passwd = $this->input->post('passwd');
}
I hope this work for you...
我希望这对你有用......
回答by Rajindra Prabodhitha
In ajax request pls use base_url('welcome/login'), like this
在 ajax 请求中请使用 base_url('welcome/login'),像这样
$.ajax({
url : "<?php echo base_url('welcome/login'); ?>"
type : "POST",
dataType : "json",
data : {"account" : account, "passwd" : passwd},
success : function(data) {
// do something
},
error : function(data) {
// do something
}
});
Use like this in controller
在控制器中像这样使用
public function login() {
$account = $this->input->post('account');
$passwd = $this->input->post('passwd');
}
I think this is work :)
我认为这是工作:)
回答by NACHIMUTHU RAMALINGAM
Homeis the controller name and login_data_submitis the function name
Home是控制器名称,login_data_submit是函数名称
$.ajax({
data:{'userName':userName,'loginpassword':loginpassword},
url:'<?php echo base_url(); ?>index.php/Home/login_data_submit',
type:'post',
success:function(data) {
alert(data);
});
Controller like
控制器喜欢
public function login_data_submit(){
$data=array(
'username'=>$this->input->post('userName'),
'loginpassword'=>$this->input->post('loginpassword'),
);
}