js代码
function AjaxInitForm(formId, btnId, isDialog, urlId){
var formObj = $('#' + formId);
var btnObj = $("#" + btnId);
var urlObj = $("#" + urlId);
formObj.Validform({
tiptype:3,
callback:function(form){
//AJAX提交表单
$(form).ajaxSubmit({
beforeSubmit: formRequest,
success: formResponse,
error: formError,
url: formObj.attr("action"),
type: "post",
dataType: "json",
timeout: 60000
});
return false;
}
});
//表单提交前
function formRequest(formData, jqForm, options) {
btnObj.prop("disabled", true);
btnObj.val("提交中...");
}
//表单提交后
function formResponse(data, textStatus) {
if (data.status == 1) {
btnObj.val("提交成功");
//是否提示,默认不提示
if(isDialog == 1){
$.dialog.tips(data.msg, 2, "32X32/succ.png", function(){
if(data.url){
location.href = data.url;
}else if(urlObj.length > 0 && urlObj.val() != ""){
location.href = urlObj.val();
}else{
location.reload();
}
});
}else{
if(data.url){
location.href = data.url;
}else if(urlObj){
location.href = urlObj.val();
}else{
location.reload();
}
}
} else {
$.dialog.alert(data.msg);
btnObj.prop("disabled", false);
btnObj.val("再次提交");
}
}
//表单提交出错
function formError(XMLHttpRequest, textStatus, errorThrown) {
$.dialog.alert("状态:" + textStatus + ";出错提示:" + errorThrown);
btnObj.prop("disabled", false);
btnObj.val("再次提交");
}
}
前台
<script type="text/javascript">
$(function () {
//初始化评论列表
AjaxPageList('#comment_list', '#pagination', 10, @ViewData["comment_count"], '/Base/Process?action=comment_list&article_id=@ViewData["article_id"]', '/images/user_avatar.png');
//初始化发表评论表单
AjaxInitForm('comment_form', 'btnSubmit', 1);
});
</script>
<div class="comment_box">
<h3 class="base_tit">
<span><a href="#Add">发表评论</a></span>共有 @ViewData["comment_count"]访客发表了评论</h3>
<ol id="comment_list" class="comment_list">
<span style="line-height: 35px;">暂无评论,快来抢沙发吧!</span>
</ol>
</div>
<div class="line20">
</div>
<div id="pagination" class="flickr">
</div>
<!--放置页码-->
<div class="comment_add">
<h3 class="base_tit">
我来说几句吧<a name="Add"></a></h3>
<form id="comment_form" name="comment_form" action="/Base/comment_add?action=comment_add&article_id=@ViewData["article_id"]" method="post">
<div class="comment_editor">
<textarea id="txtContent" name="txtContent" class="input" style="width: 658px; height: 70px;"
datatype="*" sucmsg=" "></textarea>
</div>
<div class="subcon">
<input id="btnSubmit" name="submit" class="btn right" type="submit" value="提交评论(Ctrl+Enter)" />
<span>验证码:</span>
<input id="txtCode" name="txtCode" type="text" class="input small" datatype="s4-4"
errormsg="请填写4位验证码" sucmsg=" " onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('btnSubmit').click();return false};" />
<img alt="验证码图片" width="80" height="22" id="ImgValidateCode" title="看不清楚?" style="vertical-align: middle;"
src="@Url.Action("SecurityCode")" onclick="this.src=this.src+'?'" />
</div>
</form>
</div>
[HttpPost]
public ActionResult comment_add()
{
return Json("fdsfdsfds");
}
检查请求了没,这个请求有没被发出去?一般是前端JS的问题
AJax返回正常的话,一般情况是可以执行success操作的啊,你调试下js,看看服务器端是否返回正常,你发的代码,粗略看是没有问题,这个需要你结合实际环境调试的
F12控制台查看错误.没有回调函数目测是你服务器内部错误了