I'm having a trouble in detecting ajax request in MVC 6 controller, since IsAjaxRequest is not found in MVC 6 I used the following function:
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));
var x = request.Headers["X-Requested-With"];
if (request.Headers != null)
return request.Headers["X-Requested-With"] == "XMLHttpRequest";
return false;
}
but the function always return false
my jquery code:
$("ul.menu a").click(function (e) {
e.preventDefault();
var url = $(this).attr("href");
$('#main-container').load(url);
});
-
check this may be usefulMahesh Singh Chouhan– Mahesh Singh Chouhan2017年06月02日 15:59:08 +00:00Commented Jun 2, 2017 at 15:59
-
I've already done, but the method always return false.Morz– Morz2017年06月02日 16:06:20 +00:00Commented Jun 2, 2017 at 16:06
-
Can you see from your browser if the request header contains X-Requested-With? Is the url you are loading from the same domain as the page that is loaded on the browser?rino.batin– rino.batin2017年06月05日 13:34:59 +00:00Commented Jun 5, 2017 at 13:34
1 Answer 1
The issue was in the selector, since it was wrong e.preventDefault doesn't work and the request continues normally, that's why it will never be ajax request.
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
lang-js