为什么不Request.IsAjaxRequest()在ASP.NET MVC 3工作? [英] Why doesn't Request.IsAjaxRequest() work in ASP.NET MVC 3?

查看:128
本文介绍了为什么不Request.IsAjaxRequest()在ASP.NET MVC 3工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个新的项目,asp.net MVC3用剃刀,并希望把登录到一个Ajax请求。

I'm creating a new project, asp.net mvc3 with Razor, and wanting to turn the LogOn into an ajax request.

HTML

@using (Ajax.BeginForm("LogOn", "Account", new AjaxOptions { HttpMethod="post", OnSuccess="LoginSubmitted"}))
{
}

控制器

if (Request.IsAjaxRequest())
{
    return Json(new { ResultMessage = "Username or password provided is incorrect"});
}
else
{
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
}

其他一切保持不变。

Everything else remains the same.

首先,在寻找与小提琴手的HTTP响应,我发现没有任何的x要求 - 与标头。所以,我想补充

First, looking at the the http response with Fiddler, I notice there is no x-requested-with header. So I add

<input type="hidden" name="X-Requested-With" value="XMLHttpRequest" />

这似乎工作,但现在我收到背面是一个JSON对象,这是不被解析,而不是谷歌浏览器只呈现的J​​son发送回应用程序/ JSON文档筛选。所有脚本都在发生。

That seems to work, but now what I receive back is a Json object, which isn't being parsed and instead Google Chrome is just rendering the Json to screen by sending back an application/json doc. All the scripts are in place.

我也做到了这一点:

@using (Ajax.BeginForm("Submit", "home", new AjaxOptions { HttpMethod = "Post", OnSuccess="LoginSubmitted"}))
{
}


@section head
{
    <script type="text/javascript">
        function LoginSubmitted(res) {
            alert(res.Message);
        }   
    </script>
}


    public ActionResult Submit(string id)
    {
        if (Request.IsAjaxRequest())
        {
            return Json(new { Message = "Logged In" } );
        }
        else
        {
            return View();
        }
    }

在我自己的创作,用标准的助手,工作正常的一种形式。

In a form of my own creation, which works fine using the standard helpers.

这是怎么回事?

推荐答案

这是因为默认情况下ASP.NET MVC 3使用MicrosoftAjax *库jQuery和不显眼的AJAX来代替。这意味着,当你写 Ajax.BeginForm ,你将需要包括在你的页面正确的脚本:

That's because by default ASP.NET MVC 3 uses jQuery and unobtrusive AJAX instead of the MicrosoftAjax* library. This means that when you write Ajax.BeginForm you will need to include the proper scripts inside your page:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

在你的web.config请确保您已启用非侵入式JavaScript:

and in your web.config make sure that you have unobtrusive javascript enabled:

<add key="UnobtrusiveJavaScriptEnabled" value="true"/> 

现在你可以放心地扔掉所有的 MicrosoftAjax * 如果您有任何网页上的脚本引用,他们不再使用。

Now you can safely throw away all MicrosoftAjax* script references on your page if you had any, they are no longer used.

这是说,我个人从来没有使用过任何的阿贾克斯。* 帮手。我总是preFER有所控制。所以,我会写:

This being said personally I never used any of the Ajax.* helpers. I always prefer to have control. So I would write:

@using (Html.BeginForm("LogOn", "Account"))
{
}

,然后AJAXify使用的jQuery插件的形式这种形式:

$(function() {
    $('form').ajaxForm(function(result) {
        alert('form successfully submitted');
    });
});

这篇关于为什么不Request.IsAjaxRequest()在ASP.NET MVC 3工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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