提交时保持加载但不返回数据

时间:2017-10-05 18:28:14

标签: javascript php jquery ajax codeigniter

我创建了一个用于捐赠的表单,当用户进行捐赠时他将查看错误,或者如果成功将能够看到第二页上的表单,如第二个表单将被加载。现在的问题是,当我测试数据在数据库以及后端正确更新时,一切正常。问题是ajax没有获得返回数据我已经将加载器置于成功状态,加载器将被隐藏,并且它无法正常工作,但是没有获得返回数据,任何人都可以帮我修复此错误请在这里是我的代码

这是我的javascript

$(document).ready(function() {
    $('.donate').on('submit', function(e) {
        $('.load').fadeIn(500);
        var formData = new FormData(this);
        console.log(formData);
        $.ajax({
            url         : "http://mydomainname.com/directory/user/makedonation",
            type        : "POST",
            data        : formData,
            contentType : false,
            cache       : false,
            processData : false,
            success     : function(data) {
                $('.load').fadeOut(500);
                console.log(data.status);
                $('.donation_form').html(data);
            }
        });
    });
});

这是我的控制器User.php,正在处理数据

public function makedonation() {
    $comments  = $this->input->post('comments');
    $user_data = $this->get_data->user_info($this->session->userdata('uid'));
    $class     = $this->db->get_where('classes', array('id' => $user_data->row()->membership));
    $amount    = $class->row()->ammount_pay;
    $data      = array(
        'user_id'    => $user_data->row()->uid,
        'amount'     => $class->row()->ammount_pay,
        'rcv_amount' => $class->row()->ammount_receive,
        'class_id'   => $class->row()->id,
        'comments'   => $comments
    ); 

    $rt_data = $this->data_insert->donate_user($data);
        $rcv_data = $this->get_data->user_info($rt_data['receiver_id']);
        $name     = $rcv_data->row()->first_name . " " . $rcv_data->row()->last_name;
        $view_data = array(
            'user_id'        => $user_data->row()->uid,
            'receiver_id'    => $rt_data['receiver_id'],
            'transaction_id' => $rt_data['transaction_id'],
            'comments'       => $comments,
            'name'           => $name,
            'amount'         => $class->row()->ammount_pay,
            'command'        => 'Confirm Donation'
        );

        $this->load->view('user/includes/get_data', $view_data);
}

这是将数据插入数据库的模式

public function donate_user($data) {
            $donation_data   = array(
                'user_id'         => $data['user_id'],
                'amount'          => $data['amount'],
                'receive_ammount' => $data['rcv_amount'],
                'date_deposit'    => date('m-d-Y'),
                'time_deposit'    => date('h:i A'),
                'class'           => $data['class_id'],
                'status'          => 'Submitted'
            );
            $insert_donation = $this->db->insert('donation', $donation_data);
            $transaction_id  = $this->db->insert_id();

            $rt_data = array(
                    'message'        => 'Success',
                    'transaction_id' => $transaction_id,
                    'receiver_id'    => $receiver->row()->user_id
                );

            return $rt_data;
        }

现在这是我的困惑,如果数据正确插入数据库,当我在浏览器中单独运行此文件时我指的是我的控制器第二页正在正确加载没有错误发现为什么ajax不是返回数据

1 个答案:

答案 0 :(得分:1)

这是你的答案。您必须将ajax返回数据设置为json并在控制器中执行以下操作。

$(document).ready(function() {
    $('.donate').on('submit', function(e) {
        $('.load').fadeIn(500);
        var formData = new FormData(this);
        console.log(formData);
        $.ajax({
            url         : "http://mydomainname.com/directory/user/makedonation",
            type        : "POST",
            data        : formData,
            contentType : false,
            cache       : false,
            processData : false,
dataType: 'json',
            success     : function(data) {
                $('.load').fadeOut(500);
                console.log(data.status);
                $('.donation_form').html(data.response);
            }
        });
    });
});

控制器代码

public function makedonation() {
    $comments  = $this->input->post('comments');
    $user_data = $this->get_data->user_info($this->session->userdata('uid'));
    $class     = $this->db->get_where('classes', array('id' => $user_data->row()->membership));
    $amount    = $class->row()->ammount_pay;
    $data      = array(
        'user_id'    => $user_data->row()->uid,
        'amount'     => $class->row()->ammount_pay,
        'rcv_amount' => $class->row()->ammount_receive,
        'class_id'   => $class->row()->id,
        'comments'   => $comments
    ); 

    $rt_data = $this->data_insert->donate_user($data);
        $rcv_data = $this->get_data->user_info($rt_data['receiver_id']);
        $name     = $rcv_data->row()->first_name . " " . $rcv_data->row()->last_name;
        $view_data = array(
            'user_id'        => $user_data->row()->uid,
            'receiver_id'    => $rt_data['receiver_id'],
            'transaction_id' => $rt_data['transaction_id'],
            'comments'       => $comments,
            'name'           => $name,
            'amount'         => $class->row()->ammount_pay,
            'command'        => 'Confirm Donation'
        );

        $result = $this->load->view('user/includes/get_data', $view_data,true);
$this->output->set_content_type('application/json')->set_output(json_encode(array('response' => $result,'status'=>'success')));

}

您可以在变量中读取https://www.codeigniter.com/userguide3/libraries/output.html作为输出并返回视图,根据视图的codeigniter功能将最后一个参数设置为true。