简体   繁体   English

通过 ajax/jQuery $.post 将未经身份验证的用户重定向到不同的页面

[英]Redirect unauthenticated user to different page via ajax/jQuery $.post

I have a jQuery post call like the following:我有一个 jQuery 邮寄电话,如下所示:

$.post('/Controller/_PartialAction', { 'param1': data }, function(result) { });

That's fine and it all works, except when a user's authentication cookie expires.很好,一切正常,除非用户的身份验证 cookie 过期。

What happens is, there is already stuff in there that will redirect /Controller/_PartialAction to the login page, BUT the partial action in this case is a partial view, instead I want to redirect a full view.发生的事情是,那里已经有东西可以将/Controller/_PartialAction重定向到登录页面,但是在这种情况下,部分操作是局部视图,而不是我想重定向完整视图。

So it is already redirecting that to the login page, but I want to redirect a different page, say I want to redirect to /Controller/Index .所以它已经将其重定向到登录页面,但我想重定向到另一个页面,比如我想重定向到/Controller/Index Is there any way to specify an error path or redirect path in an ajax/jQuery call?有没有办法在 ajax/jQuery 调用中指定错误路径或重定向路径?

IE, something like this (This doesn't exist, but like what i'm trying to do) IE,类似这样的东西(这不存在,但就像我正在尝试做的那样)

$.post({success}, {redirect}, {error}, {data}, {function})

So basically, how can I do a jQuery/ajax post call with a different url for errors so I can redirect to that one?所以基本上,我怎样才能使用不同的 url 进行 jQuery/ajax post 调用,以便我可以重定向到那个错误?

edit编辑

What is being redirected is the ajax call itself.被重定向的是 ajax 调用本身。 So I can't redirect it from the controller.所以我无法从 controller 重定向它。

So you want to define the action URL based on whether a user's sessions has expired?所以您想根据用户的会话是否已过期来定义操作 URL?

Why don't you define the action URL as a variable and then use it in the post call:为什么不将操作 URL 定义为变量,然后在后调用中使用它:

// Use an asp.net function to work out whether session has expired
if( ... ) {
   // Session expired
   var url = 'Controller/Index';
} else {
   // Session alive
   var url = 'Controller/_PartialAction';
}

$.post(url, { 'param1': data }, function(result) { });

I'm not entirely sure what you're after, but (based on http://api.jquery.com/jQuery.post/ ) could you use the underlying $.ajax (http://api.jquery.com/jQuery.ajax/) and set an error function?我不完全确定你在做什么,但是(基于http://api.jquery.com/jQuery.post/ )你可以使用底层的 $.ajax (http://api.jquery.com/ jQuery.ajax/) 并设置error function?

Or can you test for the expiration of the cookie in the success callback, then redirect based on the result of that test.或者您可以在成功回调中测试 cookie 的过期时间,然后根据该测试的结果进行重定向。

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

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