Jquery 如果没有contentType:';application/json';
我的问题是ajax中的success函数起作用,但error不起作用,但如果添加到contentType:'application/json'work always error函数中Jquery 如果没有contentType:';application/json';,jquery,ajax,asp.net-ajax,Jquery,Ajax,Asp.net Ajax,我的问题是ajax中的success函数起作用,但error不起作用,但如果添加到contentType:'application/json'work always error函数中 $.ajax({ type: "POST", data: { pollCodeInput: pollCodeInput, pollNameInput: pollNameInput, promotionPoints: promotion
$.ajax({
type: "POST",
data: { pollCodeInput: pollCodeInput, pollNameInput: pollNameInput, promotionPoints: promotionPoints },
url: "http://Work.local/mvc/KeyFacts/CreatePoll2",
//contentType: 'application/json',
success: function () {
openNav();
$('#modal').removeClass('modal-open');
},
error: function () {
alert("error!");
}
});
});
调试时,我的控制器工作正常。这是我的控制器
[HttpPost]
public ActionResult CreatePoll2(string pollCodeInput, string pollNameInput, int promotionPoints = 0)
{
MethodResult result = BusinessComponentRegistry.SingleInstance.PollManager.Create(pollCodeInput,
pollNameInput, 0);
if (result.HasErrors)
{
ViewBag.ErrorCreatePoll = result.Messages.ToString();
return Json(new { success = false, responseText = "The attached file is not supported." }, JsonRequestBehavior.AllowGet);
}
else
{
ViewBag.SuccessCreatePoll = result.Messages.ToString();
return Json(new { success = true, error = false, responseText = "Success!!" }, JsonRequestBehavior.AllowGet);
}
}
首先,请将您的all参数设置为object
var obj = {};
obj.first_name = $("#FirstName").val();
obj.last_name = $("#LastName").val();
obj.email_id = $("#Email").val().toLowerCase();
obj.gender = $("#Gender").val();
obj.birth_date = $("#BirthDate").val();
obj.address = $("#Address").val();
obj.cell_phone = $("#Phone").val();
obj.user_name = $("#UserName").val();
obj.password = $("#Password").val();
obj.security_question = $("#SecurityQuestion").val();
obj.security_answer = $("#SecurityAnswer").val();
obj.role_id = $("#EmployeeType").val();
obj.access_level = $("#AccessLevel").val();
obj.business_id = businessId;
obj.isActive = "true";
obj.teamlead_id = $("#TeamleadName").val();
obj.projectlead_id = $("#ProjectleadName").val();
obj.manager_id = $("#ManagerName").val();
现在用这个json方法转换它们
dataValue = JSON.stringify(obj);
像这样使用它
$.ajax({
type: "POST",
url: strUrl,
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: dataValue,
async: false,
success: onSuccess,
error: function (err) {
callLoader(false);
swal({
title: "Something Wents Wrong", text: "", type: "error",
showCancelButton: false, closeOnConfirm: true, confirmButtonText: "OK",
}, function (isConfirm) {
window.location = "signin";
});
// console.log(err);
}
});
}
问题是ActionResult返回OK(http 200),因为两个分支都返回有效的
Json
return Json(new ...
因此,ajax
调用总是命中success:
回调
在您的操作中,您处理PollManager没有正确创建的情况,并将成功结果返回给ajax
在该结果中有一个标志,表示“success”为false,但这与success:
回调使用的成功不同-这里没有按约定进行配置。你可以很容易地将该值称为其他值,例如:
return Json(new { workedlikeacharm = false ...
因此,这里最好的(IMO)选项是保持操作的原样,向ajax返回一个有效的结果,并用一个标志指示要做什么,然后在成功:
回调中处理它,例如:
success:function(result) {
if (result.success == true) {
openNav();
$('#modal').removeClass('modal-open');
} else {
alert(result.responseText);
}
但是,如果您希望保持ajax成功/失败并更改操作,则需要更改返回的内容:
if (result.HasErrors)
return new HttpStatusCodeResult(500, "file is not supported");
由于现在返回的是500而不是200(200与有效的返回新Json(..
),因此javascript代码将命中错误:
回调
或者,您可以只抛出一个异常(将生成500状态代码结果),例如:
在这两种情况下,如果要查看消息,必须更新
错误处理程序以读取http状态消息。检查如何从错误处理程序获取一些有用的信息,而不是只发出“错误”警报如果返回一个成功的结果,为什么您会期望错误处理程序启动?仅仅因为成功的结果包含一个布尔值,它仍然是一个成功。您要么需要在成功:
处理程序中处理success=false
,要么不返回有效的(200)结果,例如if(result.HasErrors)抛出新的invalidooperation(
notsupported')`您是否尝试过这一个“contentType:”application/json;charset=utf-8“,”实际上,我不太理解,但if(result.hasrerrors)函数工作例如:用户输入空或现有的pollCode,它是正确的工作,即使显示的成功函数不保存现有的pollCode或空pollCode谢谢你,我意识到了问题,现在来编辑我的文章,但这个答案真的很清楚,可以理解
if (result.HasErrors)
throw new InvalidOperationException("file is not supported");