Request.IsAjaxRequest returns false

I have an MVC project in which I have a form with a submit button. I have added a client side handler jquery that catches the form submit event. The javascript function invokes the same MVC action that would be invoked without javascript.

 $("form[action ='/List/CreateItem']").submit(
            function() {
                $.post($(this).attr("action"), $(this).serialize(), function(response) { $("#results").html(response); });
                return false;
            }
            );

In the called MVC action, I test Request.IsAjaxRequest to decide whether to return a view or JSON result. My problem is that Request.IsAjaxRequest returns false, although I know that the call is made from the jquery function. (I know this because if I comment out the $ .post line in the jquery function and just leave the returned false line, nothing will happen. If I don't comment on the line, the action will be executed, but it will return the view because IsAjaxRequest is false. )

Should this line force Request.IsAjaxRequest to be true?

+4
source share
4 answers

The property Request.IsAjaxRequestshould reflect the existence of the HTTP header X-Requested-With. Is this header really sent to the server? As James suggests, try talking about it with Fiddler or similar proxy alternatives.

+3
source

Well ... sorry. I do not know what has changed, but now IsAjaxRequest returns true. I am comparing the code that I wrote above and what is being executed now, and I do not see the difference. I have cheated on this many times before, and now I have repeatedly become faithful. Of course, I am missing something, but I do not see it.

+1

, , , HTTP-, Fiddler (www.fiddlertool.com), .

0
0

All Articles