簡體   English   中英

ajax 函數不會進入 php codeigniter 控制器

[英]ajax function not going to php codeigniter controller

我寫了一個 ajax 函數,它發布了不同的數字。 這是ajax函數。

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 ,所以基本上 url 是 addUserChildrenCash/2 但它不會轉到 codeigniter 控制器並更改頁面。 這是控制器功能。

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發送它

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

所以你的代碼應該是

在阿賈克斯

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
}
self.giveCashtoChild = function(){
            var id = self.selectedchild();
            $.ajax({
                type: 'POST',
                url: BASEURL + '/index.php/main/addUserChildrenCash/"+id,
                contentType: 'application/json; charset=utf-8'
            })
            .done(function() {

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

在您的 codeigniter 用例中,我會像這樣傳遞您的 ajax 參數:

 $.ajax({
   type: 'post',
   url: BASEURL +'/index.php/main/addUserChildrenCash',
   data: { 'childID': self.selectedchild() },
   })
  ...

在 codeigniter 控制器中,我會收到這樣的參數:

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

您還應該檢查您的 BASEURL 是否分配了正確的值。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM