Why doesn't Request.IsAjaxRequest() work in ASP.NET MVC 3?
Posted
by
Rob Ellis
on Stack Overflow
See other posts from Stack Overflow
or by Rob Ellis
Published on 2011-03-02T07:21:55Z
Indexed on
2011/03/02
7:24 UTC
Read the original article
Hit count: 266
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"}))
{
}
Controller
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.
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" />
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.
Whats happening?
© Stack Overflow or respective owner