JQuery Ajax函数错误时确实有成功
我的代码调用了error函数,即使它按预期命中了操作url。以下是Jquery ajax代码:JQuery Ajax函数错误时确实有成功,jquery,ajax,asp.net-mvc,razor,Jquery,Ajax,Asp.net Mvc,Razor,我的代码调用了error函数,即使它按预期命中了操作url。以下是Jquery ajax代码: function ContactDialog(action, controller) { var url = '/' + action + '/' + controller; $("#ContactDialog").dialog({ autoOpen: true, hide: "fade", show: "bounce", height: $(window)
function ContactDialog(action, controller) {
var url = '/' + action + '/' + controller;
$("#ContactDialog").dialog({
autoOpen: true,
hide: "fade",
show: "bounce",
height: $(window).height() / 2,
width: $(window).width() / 2,
title: "Send an email",
buttons: [{
text: "Send", click: function () {
$.ajax({
url: url,
data: $("form").serialize(),
context: this,
contentType: 'application/html; charset=utf-8',
dataType: "json", //xml, json, script and html
success: function () {
$(this).dialog("close");
sendConfirmMessage('msgSent');
},
error: function () {
sendConfirmMessage('msgNotSent');
}
});
},
}, {
text: "Cancel", click: function () {
$(this).dialog("close");
}
}]
});
下面是html/razor代码:
<div style="margin-top: 10px; visibility: hidden;" id="contactLink">
<h3><a href="#" onclick="ContactDialog('SendEmail', 'SendMail')">Contact me</a></h3>
</div>
<div id="ContactDialog" style="display: none;">
@using (Ajax.BeginForm(null, null, new AjaxOptions { UpdateTargetId = "UpdaterDiv" }, new { id = "contactForm" }))
{
@Html.LabelFor(model => model.subject)
@Html.TextBoxFor(model => model.subject, new { @class = "AboutControls" })
<br />
@Html.ValidationMessageFor(model => model.subject)
<br />
@Html.LabelFor(model => model.from)
@Html.TextBoxFor(model => model.from, new { @class = "AboutControls" })
<br />
@Html.ValidationMessageFor(model => model.from)
<br />
@Html.LabelFor(model => model.body)
@Html.TextAreaFor(model => model.body, new { @class = "AboutControls AboutControlTxtArea" })
<br />
@Html.ValidationMessageFor(model => model.body)
<hr />
<br /><br />
}
</div>
<div style="display: none;" title="Info" id="msgSent">
<p>Thank you, your message has been sent </p>
</div>
<div style="display: none;" title="Info" id="msgNotSent">
<p>There was an error sending you message, please try again </p>
</div>
这里有一些东西可能会有帮助。。。。如果我错了,人们会纠正我的!成功函数和错误函数似乎缺少一些参数。理想情况下,我们至少应该将
数据
作为参数传递
$.ajax({
url: url,
data: $("form").serialize(),
context: this,
contentType: 'application/html; charset=utf-8',
dataType: "json", //xml, json, script and html
success: function (data) {
console.log("Here is the the data - ", data);
$(this).dialog("close");
sendConfirmMessage('msgSent');
},
error: function (data) {
console.log("Oops, didn't work - ", data);
sendConfirmMessage('msgNotSent');
}
});
我也会注销一些信息以查看进度。希望这有帮助 您的ajax选项指定了
数据类型:“json”,
,但是您调用的方法返回部分视图(html)。将选项更改为
dataType: "html",
旁注:使用UrlHelper
构建URL
var url = '@Ul.Action(action, controller)';
我想我错了,我们成功了。那么,jQueryAjax的成功之处是什么呢?调试我的服务器端代码,它返回它应该返回的视图,并且它的行为符合预期,没有异常。这是解决方案,谢谢。
var url = '@Ul.Action(action, controller)';