weixin_33709219 2015-10-11 00:22 采纳率: 0%
浏览 25

Ajax帖子和代码点火器

I need to send 2 values to my codeigniter controller complete the request. But I failed in all attempts.

My Controller need to receive:

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

        });
    });
});

But the post was not send to controller. What i'm doing wrong? If i put these values in input, works fine, but I don't wanna to do that, because this is not the right way.

  • 写回答

1条回答 默认 最新

  • Memor.の 2015-10-11 01:41
    关注

    Try the below with base_url()

    Make sure you have setup the base_url in config.php

    Goto: application->config->config.php

    Also auto load the url helper

    Goto: application->config->autoload.php

    And try the below code,

    $(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.")
                }
    
            });
        });
    });
    
    评论

报告相同问题?