在ajax处理过程中,是否可以重定向到控制器中的不同位置?我的意思是我发送了一个AJAX请求,但是在它在控制器中的处理过程中,我发现我想要取消ajax请求并重定向到完全不同的位置。我在我的客户表单中使用它。它的工作原理是这样的:有人点击我的表单上的create按钮,它通过ajax检查表单的属性,如果它们是错误的,它会返回错误,否则它会重定向到新的客户。有什么想法吗?
发布于 2011-11-06 06:42:28
我发现了一些更好的东西,所以下面是你需要添加到application_controller中的代码:
def redirect_to(options = {}, response_status = {})
if request.xhr?
render(:update) { |page| page.redirect_to(options) }
else
super(options, response_status)
end
end
然后在实际操作中使用标准redirect_to
发布于 2011-10-25 22:01:51
您不能从控制器重定向,但在完成属性检查后,如果成功,则返回" success“,checking.You可以在jquery中检查该值是否为success或not.Refer下面的示例,以便更好地理解。
$.post(
"http://localhost/photo_tool/index.php/photo_tool/login",
{
username:$('#username').val(),
password:$('#password').val()
},
function(result){
if(result=='success')
{
window.location.href = 'Your URL here';
//You can also use window.location.replace("Your URL here");
}
else
{
$('#error').html(result);
}
});
在上面的例子中,控制器在成功登录时返回'success‘。
https://stackoverflow.com/questions/7890041
复制相似问题