Request.IsAjaxRequest返回false [英] Request.IsAjaxRequest returns false

查看:1124
本文介绍了Request.IsAjaxRequest返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,我有一个提交按钮形式的MVC项目。我已经加入了拦截表单提交事件jQuery的客户端处理程序。 JavaScript函数调用,将被称为没有JavaScript的相同动作MVC。

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;
            }
            );

在MVC的行动被称为,我测试Request.IsAjaxRequest来决定是否返回一个视图或JSON结果。我的问题是Request.IsAjaxRequest是返回false,尽管我知道呼叫正在从jQuery功能的。 (我知道这是因为,如果我在jQuery函数注释掉.post的$线和刚刚离开返回错误路线,没有任何反应。如果我取消注释行,动作得到执行 - 而是因为IsAjaxRequest是它返回的视图假的。)

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.)

如果这条线的事业Request.IsAjaxRequest是真的?

Should this line cause Request.IsAjaxRequest to be true?

推荐答案

Request.IsAjaxRequest 属性应反映存在的 X-请求 - 随着 HTTP标头。难道这头实际发送到服务器?正如詹姆斯所暗示的,尝试用提琴手或类似的代理服务器替代资料这个。

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.

这篇关于Request.IsAjaxRequest返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆