【问题标题】:Laravel routes do not work with jsLaravel 路由不适用于 js
【发布时间】:2015-08-21 20:31:14
【问题描述】:

我的代码有问题。不知道到底出了什么问题。

这个路由应该显示来自输入的 Token 数据,但是提交按钮只是加载并且没有任何反应。因此使用检查元素 - 生成令牌。 这是路线:

Route::get('buy', function(){
    return View('pages.add_creditcard');
});

Route::post('buy', function(){
 dd(Input::all());
});

这是视图(刀片):

{!! Form::open(['id' => 'billing-form', 'url' => '/buy', 'method' => 'post']) !!}
卡号: CVC: 截止日期: {!! Form::selectMonth(null, null, ['data-stripe' => 'exp-month']) !!} {!! Form::selectRange(null,date('Y'),date('Y')+10, null, ['data-stripe' => 'exp-year']) !!}

{!! Form::submit('立即购买', ['id' => 'submit']) !!}

{!! Form::close() !!}

这里是js文件:

(function() {

var StripeBilling = {
    init: function() {
        this.form = $('#billing-form');
        this.submitButton = this.form.find('input[type=submit]');
        this.submitButtonValue = this.submitButton.val();

        var stripeKey = $('meta[name="publishable-key"]').attr('content');

        Stripe.setPublishableKey(stripeKey);

        this.bindEvents();
    },

    bindEvents: function() {
        this.form.on('submit', $.proxy(this.sendToken, this));
    },

    sendToken: function(event) {
        this.submitButton.val('One Moment...').prop('disabled', true);

        Stripe.createToken(this.form, $.proxy(this.stripeResponseHandler, this));


        event.preventDefault();
    },

    stripeResponseHandler: function(status, response) {

        if(response.error) {
            this.form.find('.payment-errors').show().text(response.error.message);
            return this.submitButton.prop('disabled', false).val(this.submitButtonValue);
        }


        $('<input>', {
            type: 'hidden',
            name: 'stripeToken',
            value: response.id

        }).appendTo(this.form);

        //this.form[0].submit();


    }

};

StripeBilling.init();

})();

这个表单可以正常工作,但是 POST 方法什么也不做。也许js是问题?

【问题讨论】:

  • 当我将 this.form = $('#billing-form') 更改为 this.form = $('.billing-form') POST 正在工作。它生成一个令牌并将其显示在帖子页面上
  • 使用stripe.js,信用卡信息不会打到服务器,你会得到一个token。
  • 是的!但是当我添加信用卡并点击提交按钮时,它正在加载并创建令牌。我可以在检查元素上将其视为隐藏元素。但是为什么这条 route::post 不起作用???
  • 我想通过post获取购买页面的token信息

标签: javascript php jquery laravel


【解决方案1】:

Stripe.js 会创建令牌,但您仍然需要为您的购买后创建一个 ajax 调用。试试这个

if(response.error) {
}else
{
  var token = response.id;
  var last4 = response.card['last4'];
  var stripeId = response.card['id']; 
  $.post('/stripe/account/proccess_payment', {
    stripeToken : token,
    last_four : last4,
    stripe_id : stripeId
   }).done(function(data){
      // your code here
   });
}

【讨论】:

    猜你喜欢
    • 2015-02-05
    • 2019-04-24
    • 2020-05-15
    • 1970-01-01
    • 2016-09-07
    • 2019-10-05
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多