Codeigniter如何接收控制器中的ajax post数据 [英] How does Codeigniter receive the ajax post data in controller

查看:34
本文介绍了Codeigniter如何接收控制器中的ajax post数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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"];
}

现在我可以通过数组索引获取帐户和密码,但是如何将接收到的数据转换为对象,以便获得如下属性:$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 post数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆