I have a codeigniter file in controller . I want to pass parameter in constructor from ajax using jquery . How can pass value in $action ???
function __construct($action)
{
$this->json['status']=false;
if (preg_match("/json/i", $_SERVER['HTTP_ACCEPT'], $match))
$this->requestType = "JSON";
switch ($action) {
case 'login': $this->login($_REQUEST);break;
case 'addSocialPages': $this->setSocialLinks($_REQUEST);break;
case 'get_info': $this->setSocialinfo($_REQUEST);break;
case 'loginfromfront': $this->loginFront($_REQUEST);break;
default:
# code...
break;
}
if($this->requestType=="JSON")
echo json_encode($this->json);
}
答案 0 :(得分:0)
你不能真的这样做。
控制器由核心系统构建,您无法在初始化时触摸它们的构造函数(核心代码:$ CI = new $ class(); //调用时没有参数)。构造函数更倾向于初始化事物而不是为了努力工作。
顺便说一句,你的概念并没有用(在构造函数中做所有事情)。
CodeIgniter有自己的方式来运行/处理请求,如下所示:
class Default extends CI_Controller {
protected function send_json_output($response) {
$this->output->set_content_type('application/json');
echo json_encode($response);
}
public function login() {
$username = $this->input->post('username');
$password = $this->input->post('password');
// your login code
$response = array('succes' => true);
$this->send_json_output($response);
}
public function get_info() {
$user_id = $this->input->post('user_id');
// your get_info code
$user_name = $this->get_username_by_id($user_id);
$response = array('username' => $user_name);
$this->send_json_output($response);
}
public function login_from_front() {
// your login_from_front code
}
}
JS
// call the login action:
jQuery.post(
'<?php echo site_url('default/login'); ?>',
{username:'frank', password:1234},
function(data) {
if(data.success) {
alert('You are logged in');
}
},
'json'
);
// call the get_info action:
jQuery.post(
'<?php echo site_url('default/get_info'); ?>',
{user_id:123},
function(data) {
alert(data.username);
},
'json'
);