开发者

Request.IsAjaxRequest returns false

开发者 https://www.devze.com 2022-12-10 18:44 出处:网络
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

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?


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.


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.


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.


Does CreateItem perform redirect? See Firefox does not preserve custom headers during Ajax request redirect: an ASP.NET MVC solution

0

精彩评论

暂无评论...
验证码 换一张
取 消