我正在尝试写一个直截了当的评论海报。我在 Controller 中有这段代码:
[HttpPost]
[ValidateInput(false)]
public ViewResult Comments(MemberData md, long EntryId, string Comment, long LastId = 0)
{
bool isModerated = true;
bool isLoggedIn = GenesisRepository.IsNotGuest(md.MemberGUID);
bool isCommentAllowed = GenesisRepository.IsPermissionAssigned(md.MemberGUID, "Comments", "Create");
// Moderate comment?
if (moderateGuestComments == false && isLoggedIn == false) isModerated = false;
if (moderateMemberComments == false && isLoggedIn) isModerated = false;
long memberId = (from m in GenesisRepository.Member
where m.MemberGUID == md.MemberGUID
select m.MemberID)
.FirstOrDefault();
if (
EntryId > 0
&& !string.IsNullOrEmpty(Comment)
&& memberId > 0
&& isCommentAllowed)
{
Comments comment = new Comments {
Comment = Comment,
Date = DateTime.Now,
isActive = isModerated ? false : true,
MemberID = memberId,
StreamEntryID = EntryId,
};
if (GenesisRepository.SaveComment(comment))
{
List<Comments> comments = new List<Comments>();
comments = (from c in GenesisRepository.Comments
where c.StreamEntryID == EntryId
&& c.comID > LastId
select c
).ToList();
return View("DisplayComments", comments);
}
}
return View("CommentError", "Unable to post comment.");
}
当一切正常并且操作返回时 return View("DisplayComments", comments);
$.post()
成功函数被触发。但是,当操作返回 return View("CommentError", "Unable to post comment.");
$.post()
ajax 失败。我不明白为什么 $.post()
关心我返回的是哪个 View 。
这是我的 Javascript:
<script type="text/javascript">
$(document).ready(function () {
$("#comments").ajaxError(function (event, request, settings) {
alert("Error requesting page " + settings.url);
});
$("button#submitComment").click(function () {
var commentList = $("#comments");
var lastId = $(".comment h4").last().attr("id");
var commentData = "EntryId=" + $("input#EntryID").val()
+ "&Comment=" + $("textarea#Comment").val()
+ "&LastId=" + lastId;
$.post(
"/find/Comments/Comments",
commentData,
function (data) {
alert("success");
alert(data);
if ($(data).filter(".error").length > 0) {
error = $(data);
$(this).after(error);
}
else {
newComments = $(data);
newComments.filter(".comment").css('display', 'none');
alert(newComments);
commentList.append(newComments);
$(".comment").each(function () {
$(this).slideDown("fast")
});
$("#Comment").attr("value", "");
}
}
);
});
});
</script>
这会导致 ajax 失败吗?
这是两个 View 的样子:
View("DisplayComments", comments); (作品)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<List<Genesis.Domain.Entities.Comments>>" %>
<% foreach (var item in Model) %>
<% { %>
<div class="comment" style="background:#eee; border:1px solid gray; padding:10px 10px 0 10px; margin-bottom:20px;">
<h4 id="<%:item.comID %>"><%: item.Member.ScreenName%> commented on <%: String.Format("{0:f}", item.Date)%></h4>
<p>
<%: item.Comment%>
</p>
</div>
<% } %>
View("CommentError", "无法发表评论。"); (不起作用)
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<string>" %>
<div class="error">
<%:Model%>
</div>
这会导致 ajax 发布失败吗?
最佳答案
如果 ajaxError
函数被触发,这强烈表明您的 Controller 操作返回的状态代码不同于 200,可能是 500,这强烈表明您的 Controller 操作在到达最后一个之前抛出异常行并能够返回 View 。
所以这里是要做的步骤:
- 使用FireBug
- 查看您的服务器发送什么作为对 AJAX 请求的响应
- 解析响应状态码和响应内容
替代方法:
- 在您的 Controller 操作中放置一个断点
- 点击F5
- 当点击 Controller 操作时逐步执行您的代码
- 准确观察发生的事情
备注:我强烈建议您正确编码您的 AJAX 输入。所以不是:
var commentData = "EntryId=" + $("input#EntryID").val()
+ "&Comment=" + $("textarea#Comment").val()
+ "&LastId=" + lastId;
你绝对应该:
var commentData = $.param({
EntryId: $("input#EntryID").val(),
Comment: $("textarea#Comment").val(),
LastId: lastId
});
请注意,每次在处理查询字符串参数时使用+
、&
和=
符号(无论您使用什么语言)你做错了。
关于javascript - 为什么我的 jQuery.post() ajax 在我返回一个 View 时失败,但在我返回另一个 View 时却没有失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4521747/