我看到了一些与这个问题相关的帖子,但没有找到任何好的解决方法来解决这个问题。与我的应用程序的其余部分一样,我希望能够支持启用JavaScript和禁用JavaScript的情况,这就是为什么我想检测请求是否为ajax的原因。
我意识到使用的iframe方法在技术上并不是ajax,但我正在尝试模仿ajax效果。
欢迎任何建议。
从ASP.NET MVC 2开始(以及之后的版本),在Request
上有一个扩展方法。
if (Request.IsAjaxRequest())
{
// was an ajax request
}
使用像.load()这样的 jQuery 方法调用控制器方法将导致 Request.IsAjaxRequest()
返回 true。
你需要为“IsAjaxRequest”方法设置“X-Requested-With”标题,以使其返回真。以下是在jquery中实现此操作的方法。
$(document).ready(function() {
jQuery.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
return xhr;
}
});
});
我刚意识到我完全没有回答问题,所以我在这里添加内容,并保留我的旧答案:
问题在于,在通过iFrame上传文件时,"X-Requested-With"头未设置,并且您无法在JavaScript中为普通表单POST设置特定的请求头。您将不得不采用其他技巧,比如使用POST发送包含值的隐藏字段,然后更改或覆盖“IsAjaxRequest”扩展方法,也检查此条件。 如何覆盖现有的扩展方法?
可能最好的选择是在默认的MVC扩展方法代码基础上包含自己的扩展方法,并更改以检测您的iFrame上传POST,然后在需要使用它的任何地方都使用您的扩展方法。
实际上,jQuery默认将'X-Requested-With'头设置为'XMLHttpRequest'。 只要您小心地使用所有AJAX调用,它就非常有用。
根据您的需求,很容易在操作过滤器中设置检测以在需要时使用它,甚至可以将其构建到控制器类中,如下所示:
[jQueryPartial]
public abstract class MyController : Controller
{
public bool IsAjaxRequest { get; set; }
}
动作过滤器特性:
public class jQueryPartial : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// Verify if a XMLHttpRequest is fired.
// This can be done by checking the X-Requested-With
// HTTP header.
MyController myController = filterContext.Controller as MyController;
if (myController != null)
{
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != null
&& filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
{
myController.IsAjaxRequest = true;
}
else
{
myController.IsAjaxRequest = false;
}
}
}
}
使用该实现:
public class SomeController : MyController
{
public ActionResult Index()
{
if (IsAjaxRequest)
DoThis();
else
DoThat();
return View();
}
}
var requestWith = filterContext.HttpContext.Request.Headers["X-Requested-With"]; myController.IsAjaxRequest = requestWith == "XMLHttpRequest;"
- Devin Burke我刚偶然发现了这个问题,后来找到了更好的解决方案,所以在此为每个从谷歌搜索到这里的人提供以下解决方法:
jQuery表单插件有两个选项可能会对此有帮助。
如果您正在使用post请求,则似乎始终使用iframe,因此如果您不需要文件上传,请在Form选项中设置"iframe = false"可以解决问题。
如果您需要iframe进行文件上传,您可以使用data属性的选项来欺骗
IsAjaxRequest()设置 : data: { "X-Requested-With": "XMLHttpRequest" }
具有两个选项的完整脚本看起来像这样:
$('#someform').ajaxForm(
{
dataType: 'json',
success: onSuccess,
error: onError,
iframe: false
data: { "X-Requested-With": "XMLHttpRequest" }
}
);
$httpProvider.defaults.headers.common = { 'X-Requested-With' : 'XMLHttpRequest' };
- parliament