ajax函数不会去php codeigniter控制器 [英] ajax function not going to php codeigniter controller

查看:54
本文介绍了ajax函数不会去php codeigniter控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个发布不同数字的ajax函数。
这是ajax函数。

I have a ajax function written which is posting different numbers. Here is the ajax function.

self.giveCashtoChild = function(){
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash'+"/"+self.selectedchild(),
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

            })
            .fail(function(xhr, status, error) {
                alert(status);
            })
            .always(function(data){                 
            });
        }

self.selectedchild()的值为2,所以基本上网址是addUserChildrenCash / 2然后它不会转到codeigniter控制器并更改页面。这是控制器功能。

self.selectedchild() has the value of 2 , so Basically the url is addUserChildrenCash/2 but then it does not go to the codeigniter controller and change the page. Here is the controller function.

public function addUserChildrenCash($childID){
            if (!$this->session->userdata('user_id')){
                redirect('main'); // the user is not logged in, redirect them!
            }

                $userid= $this->session->userdata('user_id');
                $this->load->model('main_page');
                $childname =  $this->main_page->getChildName($childID, $userid);

                $data = array(
                    'name' => $childname['children_name']
                );
                $this->load->view('header2_view');
                $this->load->view('add_user_children_cash_view' , $data);
                $this->load->view('footer_view');

        }


推荐答案

你定义ajax为 POST 但您通过 GET发送

You define ajax as POST but you sending it via GET

 type: 'POST',
 url: BASEURL + '/index.php/main/addUserChildrenCash'+"/"+self.selectedchild(),

所以你的代码应该是

在Ajax中

var id = self.selectedchild(); # Assign data to here
$.ajax(
    {

        type:"post",
        url: "<?php echo base_url(); ?>index.php/main/addUserChildrenCash", 
        data:{ id:id},
        success:function()
        {

        }
        error:function()
        {

        }
        always:function(){

        }
    });

在控制器中

public function addUserChildrenCash()
{
    $id = $this->input->post("id");
    echo $id
}

这篇关于ajax函数不会去php codeigniter控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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