在Ajax调用重复头 [英] Duplicate header on ajax call

查看:165
本文介绍了在Ajax调用重复头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喜已经创建了一个执行一个Ajax调用到控制器,以更新的ID UpdateCart.The问题的跨度是,如果用户不authentificated他得到的发送到登录页面的链接,这大干快上生成页:

Hi have created a link that executes an ajax call to a controller in order to update a span with the id UpdateCart.The problem is that if the user is not authentificated he get's sent to the Login page and this gets generated on the page:

因为它可以从图片中可以看出一些我的整个头标记如何被复制并跨度tag.This内添加是我的code:

As it can be seen from the images some how my entire header tag gets duplicated and added inside the span tag.This is my code:

    @Ajax.ActionLink("Add To Cart" ,
                                 "AddToCart" ,
                                 "Products", 
                                 new {
                                        ProductId = @products.ElementAt(0).Value
                                     },
                                 new AjaxOptions{

                                                   InsertionMode = InsertionMode.Replace,
                                                   UpdateTargetId = "UpdateCart",
                                                   HttpMethod = "GET"
                                                })
public ActionResult AddToCart(string ProductId)
        {
            if( User.Identity.IsAuthenticated ) {
                string username = User.Identity.Name;

                CartHelperClass.AddToCart(ProductId , username);
                ViewBag.ItemsInCart = CartHelperClass.CountItemsInCart(username);

                return PartialView("_AddToCart");
            } else {
                return RedirectToAction("LogIn" , "Account" , new {
                    returnUrl = "Products"
                });
            }     
        }

如何停止重复标题的创造?

How can I stop the creation of a duplicate header?

推荐答案

看看的<一个href=\"http://haacked.com/archive/2011/10/04/$p$pvent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx\"相对=nofollow> 下面的博客文章 。在这篇文章中菲尔哈克说明如何可以在表单验证模块配置停止重定向到登录页面,未经验证的用户对于AJAX请求。你可以把它返回一个真正的401状态code到客户端AJAX请求。

Take a look at the following blog post. In this post Phil Haack explains how you could configure the Forms Authentication module to stop redirecting to the LogOn page for non-authenticated users for AJAX requests. You could make it return a real 401 status code to the client for AJAX requests.

这401然后可以很容易地截取客户端,并执行任何你想要的动作 - 如使用手动重定向浏览器登录页面的 window.location.href 方法。

And this 401 could then be easily intercepted on the client and perform whatever action you want - such as manually redirecting the browser to the LogOn page using the window.location.href method.

所以你的情况,你可以简单订阅 ajaxComplete()处理这个处理程序中,你可以测试是否响应状态是401,这意味着用户不验证,你将他重定向到登录页面。

So in your case you will simply subscribe to the ajaxComplete() handler and inside this handler you could test if the response status was 401, meaning that the user is not authenticated, and you will redirect him to the LogOn page.

所以一旦你安装AspNetHaack的NuGet(安装封装AspNetHaack ),你所要做的就是在全球范围内订阅的 .ajaxComplete() 在你的页面处理程序:

So once you install the AspNetHaack NuGet (Install-Package AspNetHaack) all you have to do is to globally subscribe to the .ajaxComplete() handler in your page:

<script type="text/javascript">
    $(document).ajaxComplete(function (e, xhr, settings) {
        if (xhr.status == 401) {
            alert('Sorry you must be authenticated in order to use this action. You will now be redirected to the LogOn page');
            window.location.href = '@FormsAuthentication.LoginUrl';
        }
    });
</script>

当然,你们中的一个设置这个你应该停止从你的AJAX控制器动作重定向,因为这是没有意义的。你的控制器动作,现在就干脆是这样的:

Of course one you setup this you should stop redirecting from your AJAX controller action because this makes no sense. Your controller action will now simply look like this:

[Authorize]
public ActionResult AddToCart(string productId)
{
    string username = User.Identity.Name;
    CartHelperClass.AddToCart(productId , username);
    ViewBag.ItemsInCart = CartHelperClass.CountItemsInCart(username);
    return PartialView("_AddToCart");
}

TODO:我真的建议你使它停止使用ViewBag重构这个 AddToCart 行动,但它传递一个强类型的视图模型到你的局部视图

TODO: I'd really recommend you refactoring this AddToCart action so that it stops using ViewBag but it passes a strongly typed view model to your partial view.

这篇关于在Ajax调用重复头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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