Chr*_*low 6 c# forms ajax firefox asp.net-mvc-3
最终编辑:
在听完Darin Dimitrov的回答之后,我发现问题最终是对Controller方法的AJAX调用UpdateForm()返回一个空字符串.这是我在遇到不同问题后不久前发现必要的修改.传递一个空字符串导致Firefox的解析器窒息(而Chrome和IE显然不在乎),所以我用空字符串替换了空字符串div.
编辑:
感谢Darin Dimitrov在下面提出的建议,我发现我遇到麻烦的原因是每当提交相关表格时都会抛出错误.

错误显示"节点无法插入层次结构中的指定点".每次提交表单时都会抛出此内容.我在POST数据中注意到它似乎认为这是一个XMLHttpRequest.这是原因(有问题的AJAX请求只是返回HTML)?这是来自Firebug的POST数据:



此错误显示"XML解析错误 - 未找到元素".
仅供参考 - 返回的HTML始终为空字符串...
我有一个在IIS7上运行的MVC3应用程序.在我的一个视图中,我有一个使用Microsoft HTML帮助函数构建的表单:
@using (Ajax.BeginForm("UpdateForm", new AjaxOptions { UpdateTargetId = "TargetDiv", InsertionMode = InsertionMode.InsertAfter, OnSuccess = "ClearTextBox" }))
{
@Html.TextArea("txtInput", new { id = "txtInput", cols = "20", rows = "5", wrap = "virtual" })
<input id="send" class="button" type="submit" value="Send"/><br />
}
Run Code Online (Sandbox Code Playgroud)
当Controller提供此视图时,这将生成以下HTML:
<form action="/RootName/ControllerName/UpdateForm" data-ajax="true" data-ajax-mode="after" data-ajax-success="ClearTextBox" data-ajax-update="#TargetDiv" id="form0" method="post">
<textarea cols="20" id="txtInput" name="txtInput" rows="5" wrap="virtual"></textarea>
<input id="send" class="button" type="submit" value="Send"><br>
</form>
Run Code Online (Sandbox Code Playgroud)
我在这里基本上要做的是获取被TextArea调用内部的文本,txtInput并在点击上面的按钮时将其附加到被Div调用的末尾,并通过方法(Javascript)在追加完成后清除文本.追加始终适用于每个浏览器; 当我在Internet Explorer或Chrome中运行时,文本的清除工作正常.但是,Firefox似乎不想调用该方法. TargetDivSendtxtInputClearTextBox()ClearTextBox()
Firefox是否与data-ajax-success表单签名中的此选项不兼容?
我试过的事情
我找到了这个人: Ajax.BeginForm没有调用onSuccess
解决方案是添加此脚本:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
我在调用这个脚本:
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)
...但我尝试将其换掉以防万一.没有快乐.
我被要求尝试更改方法调用以包含C#聊天室中的一些人的括号,以便HTML如下所示:
<form action="/WebChat/TMWC/UpdateForm" data-ajax="true" data-ajax-mode="after" data-ajax-success="ClearTextBox()" data-ajax-update="#chatText" id="form0" method="post">
<textarea cols="20" id="txtInput" name="txtInput" rows="5" wrap="virtual"></textarea>
<input id="send" class="button" type="submit" value="Send"><br>
</form>
Run Code Online (Sandbox Code Playgroud)
但这没有帮助.
C#Chat中的人还建议我用警报替换Javascript调用 - 如下所示:
<form action="/WebChat/TMWC/UpdateForm" data-ajax="true" data-ajax-mode="after" data-ajax-success="alert('yo!')" data-ajax-update="#chatText" id="form0" method="post">
<textarea cols="20" id="txtInput" name="txtInput" rows="5" wrap="virtual"></textarea>
<input id="send" class="button" type="submit" value="Send"><br>
</form>
Run Code Online (Sandbox Code Playgroud)
当Chrome弹出消息框时,Firefox不会!
在新创建的 ASP.NET MVC 3 应用程序中,状态没有重现。
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult UpdateForm()
{
return Content(DateTime.Now.ToLongTimeString());
}
}
Run Code Online (Sandbox Code Playgroud)
看法 (~/Views/Home/Index.cshtml):
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script type="text/javascript">
function ClearTextBox() {
$('textarea').val('');
}
</script>
<form action="/Home/UpdateForm" data-ajax="true" data-ajax-mode="after" data-ajax-success="ClearTextBox" data-ajax-update="#TargetDiv" id="form0" method="post">
<textarea cols="20" id="txtInput" name="txtInput" rows="5" wrap="virtual"></textarea>
<input id="send" class="button" type="submit" value="Send"><br>
</form>
<div id="TargetDiv"></div>
Run Code Online (Sandbox Code Playgroud)
在 Chrome、FF 和 IE 中完美运行。
此外,您可能希望确保 Content-Type 响应 HTTP 标头与您发送的实际响应相匹配。例如,我见过很多人application/json在响应正文中发送带有一些无效 JSON 的响应标头,这会产生更敏感的解析器来阻塞。