简体   繁体   English

Rails-AJAX POST后重定向

[英]Rails - Redirecting after AJAX POST

I'm trying to do something that I thought would be incredibly simple - subscribe a user to a Stripe subscription plan and redirect them to the root path upon success. 我正在尝试做一些我认为非常简单的事情-为用户订阅Stripe订阅计划,并在成功后将他们重定向到根路径。 Nothing fancy. 没有什么花哨。

I have 3 plans, which I define in my Rails controller, and loop through each plan to create a Stripe handler for each. 我有3个计划,我在Rails控制器中定义了这些计划,并遍历每个计划为每个计划创建一个Stripe处理程序。 The handlers in my view look like this: 我认为这些处理程序如下所示:

handlers["#{count}"] = StripeCheckout.configure({
    key: "#{ENV['stripe_publishable_key']}",
    locale: 'auto',
    token: function(token) {
        $.ajax({
            url: `/subscriptions`,
            method: 'POST',
            data: {
                "stripeToken": token.id,
                "stripeEmail": token.email,
                "stripe_price": "#{plan[:stripe_price]}",
                "plan": "#{plan[:name]}",
                "description": "Subscription to #{plan[:name].capitalize} plan"
            },
            dataType: 'json',
            success: `window.location = '/'`
        });
    }
});

The success callback is something I added recently, just to try to get it to redirect somewhere upon success. 我最近添加了成功回调,只是为了使它在成功时重定向到某个地方。 Didn't work... 没工作...

The event listener: 事件监听器:

document.getElementById("subscriptionButton-#{count}").addEventListener('click', function(e) {
    handlers["#{count}"].open({
        name: "#{plan[:name].capitalize} plan",
        description: "Sign up for #{plan[:name].capitalize} plan",
        email: "#{current_user.email}",
        amount: "#{plan[:stripe_price]}"
    });
    e.preventDefault();
});

Everything works perfectly in terms of sending the data to my subscriptions controller and the I can successfully create the subscriptions on Stripe. 就将数据发送到我的订阅控制器而言,一切工作都非常完美,我可以在Stripe上成功创建订阅。 All I need to do is redirect the user. 我需要做的就是重定向用户。

The block I'm using in my controller is: 我在控制器中使用的块是:

flash[:success] = "Successfully signed up"
puts "Got here"
respond_to do |format|
    puts request.format
    format.js { render js: "window.location = '#{root_path}';" }
end

The request.format returns application/json . request.format返回application/json I thought it would return js, since it's responding to an AJAX request. 我以为它将返回js,因为它正在响应AJAX请求。 Is this what's screwing up the redirect? 这是搞砸重定向的原因吗? If so, how does one go about redirect when the format is json? 如果是这样,当格式为json时如何进行重定向? Or am I missing something else completely? 还是我完全想念其他东西?

Just make the data type javascript 只需将数据类型设为javascript

handlers["#{count}"] = StripeCheckout.configure({
key: "#{ENV['stripe_publishable_key']}",
locale: 'auto',
token: function(token) {
    $.ajax({
        url: `/subscriptions`,
        method: 'POST',
        data: {
            "stripeToken": token.id,
            "stripeEmail": token.email,
            "stripe_price": "#{plan[:stripe_price]}",
            "plan": "#{plan[:name]}",
            "description": "Subscription to #{plan[:name].capitalize} plan"
        },
        dataType: 'script', <--- right here
        success: `window.location = '/'`
    });
  }
});

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

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