Grant
Grant

Reputation: 391

Request.IsAjaxRequest returns false

I have an MVC project in which I have a form with a submit button. I have added a jquery client-side handler that intercepts the form submit event. The javascript function calls the same MVC action that would have been called without the javascript.

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

In the MVC action that is called, I test for Request.IsAjaxRequest to decide whether to return a view or a JSON result. My problem is that Request.IsAjaxRequest is returning false, even though I know the call is being made from the jquery function. (I know this because if I comment out the $.post line in the jquery function and just leave the return false line, nothing happens. If I un-comment the line, the action gets executed - but it returns the view because IsAjaxRequest is false.)

Should this line cause Request.IsAjaxRequest to be true?

Upvotes: 4

Views: 4269

Answers (4)

Grant
Grant

Reputation: 391

Well... I apologize. I don't know what has changed, but now IsAjaxRequest is returning true. I compare the code I posted above and what is executing now and I see no difference. I repeatedly got false on this before, and now I repeatedly get true. Surely I am missing something, but I don't see it.

Upvotes: 1

Asbjørn Ulsberg
Asbjørn Ulsberg

Reputation: 8820

The Request.IsAjaxRequest property should reflect the existence of the X-Requested-With HTTP header. Is this header actually sent to the server? As James suggests, try to profile this with Fiddler or similar proxy server alternatives.

Upvotes: 3

James Alexander
James Alexander

Reputation: 6312

I'm not sure what the difference would be but a possible way to identify if there is one is to use an HTTP profiling tool like Fiddler (www.fiddlertool.com) and look for possible differences between the two calls.

Upvotes: 0

Related Questions