我正在尝试测试 jquery 帖子。我正在发布一个表单,并希望在 ajax 中更新并用 div 上的成功表单替换(似乎是一个常见的用例)。
以下代码在 Firefox 中运行良好,但在 IE 中运行良好。
一个问题是,来自 Firefox 的Request.IsAjaxRequest()为 true,但来自 IE,Request.IsAjaxRequest()返回 false
注意:我将 Thread.Sleep 放入我的控制器操作中,作为测试以确保我可以看到发生了什么。
这是我的视图代码:
<div id="contact">
<form action="/Tracker/Add" method="post">
<fieldset id="inputbox">
<p>
<label>Today's number</label> <input class="inputText" id="weight" name="weight" type="text" value="208" /></p>
<p><label>Today's Date</label> <input class="inputText" id="datepicker" name="date" type="text" value="03-Mar-2010" /></p>
<p><input type="submit" value="Enter" /></p>
</fieldset>
这是javascript / jquery代码:
<script type="text/javascript" src="../../Scripts/jquery/1.3.2/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#contact form').live('submit', function() {
//$("#Loading").fadeIn(); //show when submitting
$.post($(this).attr('action'), $(this).serialize(), function(data) {
$("#contact").replaceWith($(data));
});
return false;
});
});
</script>
这是我的控制器操作:
public ActionResult Add()
{
if (if (Request.IsAjaxRequest())
{
//firefox will hit this but IE wont
}
Thread.Sleep(5000);
return PartialView("EntryView", new MyViewModel());
}