0

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);
 });
asked Jun 2, 2017 at 15:35
3
  • check this may be useful Commented Jun 2, 2017 at 15:59
  • I've already done, but the method always return false. Commented 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? Commented Jun 5, 2017 at 13:34

1 Answer 1

0

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.

answered Jun 11, 2017 at 8:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.