简体   繁体   English

Ajax帖子和代码点火器

[英]Ajax post and Code Igniter

I need to send 2 values to my codeigniter controller complete the request. 我需要发送2个值到我的codeigniter控制器以完成请求。 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. 如果我将这些值放入输入中,效果很好,但是我不想这样做,因为这不是正确的方法。

Try the below with base_url() 尝试下面的base_url()

Make sure you have setup the base_url in config.php 确保已在config.php设置了base_url

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

Also auto load the url helper 同时自动加载url帮助器

Goto: application->config->autoload.php 转到: 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.")
            }

        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM