在Ajax发布后重定向
我希望ajaxpost上的成功转到主页。 出于某种原因,我一直做错了。 知道我应该怎么做才能解决这个问题?
window.APP_ROOT_URL = "";
阿贾克斯
$.ajax({ url: '#{addbank_bankaccts_path}', type: 'POST', beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')}, dataType: "json", data: 'some_uri=' + response.data.uri , success: function(APP_ROOT_URL) { window.location.assign(APP_ROOT_URL); } });
success: function(response){ window.location.href = response.redirect; }
希望以上内容有所帮助,因为我遇到了同样的问题
您可以使用重定向状态和重定向URL从服务器返回JSON。
{"redirect":true,"redirect_url":"https://example.com/go/to/somewhere.html"}
在你的jQuery ajax处理程序中
success: function (response) { // redirect must be defined and must be true if (response.redirect !== undefined && response.redirect) { window.location.href = response.redirect_url; } }
注意,您必须在ajax配置中设置dataType: 'json'
。 希望这有用。 谢谢!
不知道为什么,但window.location.href
对我不起作用。 我最终使用了window.location.replace
,实际上是有效的。
$('#checkout').click(function (e) { e.preventDefault(); $.ajax('/post/url', { type: 'post', dataType: 'json' }) .done(function (data) { if (data.cartCount === 0) { alert('There are no items in cart to checkout'); } else { window.location.replace('/Checkout/AddressAndPayment'); } }); });