Ajax帖子和Code Igniter

时间:2015-10-11 00:22:36

标签: php jquery ajax codeigniter

我需要向codeigniter控制器发送2个值来完成请求。但是我在所有的尝试都失败了。

我的控制器需要接收:

$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.")
            }

        });
    });
});

但帖子没有发送给控制器。我做错了什么?如果我把这些值放在输入中,工作正常,但我不想这样做,因为这不是正确的方法。

1 个答案:

答案 0 :(得分:0)

使用base_url()

尝试以下内容

确保您已在base_url

中设置config.php

转到: application->config->config.php

同时自动加载url助手

转到: 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.")
            }

        });
    });
});