Ajax post and Code Igniter


Ajax post and Code Igniter

我需要发送2个值到我的编码器控制器完成请求。但我所有的尝试都失败了。

My Controller需要接收:

$token = $this->input->post('cc_token');
$hash = $this->input->post('sender_hash');
Ajax

$(document).ready(function ({
    var sender_hash = myfunction.getSenderHash();
    var card_token = myFunction.createCardToken({
        cardNumber: "4111111111111111",
        brand: 'visa',
        cvv: "123",
        expirationMonth: "12",
        expirationYear: "2030",
        success: function (response) {
        },
        error: function (response) {
            console.log(response);
        }
    });
    $("#submit").click(function () {
        $.ajax({
            url: '<?php echo $details->id?>',
            type: 'POST',
            data: 'cc_token=' + card_token + '&sender_hash=' + sender_hash,
            beforeSend: function () {
                console.log("Your request is being processed. Wait...");
            },
            success: function (data) {
                console.log("Thank you for donating. Your request has been successfully processed!");
            },
            error: function (data) {
                console.log("There was an error sending the data. Try again.")
            }
        });
    });
});

但是帖子没有发送到控制器。我做错了什么?如果我把这些值放到input里,没问题,但我不想这么做,因为这不是正确的方式

尝试下面的base_url()

确保在config.php

中设置了base_url

Goto: application->config->config.php

也自动加载url助手

Goto: application->config->autoload.php

试试下面的代码

$(document).ready(function ({
    var sender_hash = myfunction.getSenderHash();
        var hash = '';
    var card_token = myFunction.createCardToken({
        cardNumber: "4111111111111111",
        brand: 'visa',
        cvv: "123",
        expirationMonth: "12",
        expirationYear: "2030",
        success: function (response) {
                    hash = response;
        },
        error: function (response) {
            console.log(response);
        }
    });
    $("#submit").click(function () {
        $.ajax({
            url: '<?php echo base_url();?>index.php/donate/pay/<?php echo $details->id?>',
            type: 'POST',
            data: {'cc_token':hash,'sender_hash':sender_hash},
            beforeSend: function () {
                console.log("Your request is being processed. Wait...");
            },
            success: function (data) {
                console.log("Thank you for donating. Your request has been successfully processed!");
            },
            error: function (data) {
                console.log("There was an error sending the data. Try again.")
            }
        });
    });
});