Rails-在AJAX POST之后重定向 [英] Rails - Redirecting after AJAX POST

查看:55
本文介绍了Rails-在AJAX POST之后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些我认为非常简单的事情-为用户订阅一个Stripe订阅计划,并在成功后将他们重定向到根路径.没什么.

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. Nothing fancy.

我有3个计划,我在Rails控制器中定义了这些计划,并遍历每个计划为每个计划创建一个Stripe处理程序.我认为这些处理程序如下:

I have 3 plans, which I define in my Rails controller, and loop through each plan to create a Stripe handler for each. 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...

事件监听器:

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();
});

在将数据发送到我的订阅控制器上,一切工作都非常完美,我可以在Stripe上成功创建订阅.我需要做的就是重定向用户.

Everything works perfectly in terms of sending the data to my subscriptions controller and the I can successfully create the subscriptions on 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

request.format 返回 application/json .我以为它将返回js,因为它正在响应AJAX请求.这是搞砸重定向的原因吗?如果是这样,当格式为json时如何进行重定向?还是我完全想念其他东西?

The request.format returns application/json. I thought it would return js, since it's responding to an AJAX request. Is this what's screwing up the redirect? If so, how does one go about redirect when the format is json? Or am I missing something else completely?

推荐答案

只需将数据类型设置为javascript

Just make the data type 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 = '/'`
    });
  }
});

这篇关于Rails-在AJAX POST之后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆