Codeigniter如何在控制器中接收Ajax发布数据 [英] How does Codeigniter receive the ajax post data in controller
本文介绍了Codeigniter如何在控制器中接收Ajax发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用CodeIgniter开发我的项目的前端客户端.
I'm trying to use CodeIgniter to develop the front-end client of my project.
但是带有CI的ajax使我感到困惑.
But the ajax with CI make me confused.
这是我的ajax:
$.ajax({
url : "welcome/login"
type : "POST",
dataType : "json",
data : {"account" : account, "passwd" : passwd},
success : function(data) {
// do something
},
error : function(data) {
// do something
}
});
和控制器:
public function login() {
$data = $this->input->post();
// now I can get account and passwd by array index
$account = $data["account"];
$passwd = $data["passwd"];
}
现在我可以通过数组索引获取帐户和密码,但是如何将接收到的数据转换为Object,这样我就可以得到如下属性:$data->account
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
谢谢!
推荐答案
为此更改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
}
});
更改您的控制器:
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...
这篇关于Codeigniter如何在控制器中接收Ajax发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文