我正在使用一個Ajax交呼叫分配如下傳遞形式的數據:如何解決無效的JSON基元模型錯誤?
data: {model: JSON.stringify(formData) },
的model
參數用於告訴字符串化方法來解析的形式數據,作爲相同類型的模型的關係的圖。
但是,當我將JSON布爾值返回給ajax方法時,我得到了源自500內部服務器錯誤的JSON primitive model is invalid
錯誤。我認爲這也會導致我的AJAX代碼中的success function
被觸發。
問: 如何在返回布爾值來解決無效的JSON參數錯誤?
AJAX的方法:
var formData = $("createForm").serialize();
$.ajax({
type: "POST",
url: '@Url.Action("Index", "CreateEscalation")',
data: {model: JSON.stringify(formData) },
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (json) {
if (json.Success) {
window.location.href = json.redirectUrl;
}
else{
$('#submitStatus').text("Error occurred while processing your request, please try again or contact system administrators");
$(this).addClass('alert alert-danger fade in');
$('#submitStatus').show();
}
},
error: function (jqXHR, exception) {
}
});
控制器交方法:
[HttpPost]
public ActionResult Index(Escalation escalation)
{
try
{
bool success = sqlConnection.InsertWebReq(escalation);
if (success)
{
return Json(new
{
redirectUrl = Url.Action("Index", "EscalationHistory"),
Success = true
});
}
else
{
return Json(new
{
Success = false
});
}
}
catch (Exception ex)
{
return Json(new
{
Success = false
});
}
}
刪除'的contentType:
以下的Ajax方法定義在完成了爲我工作 「應用/ JSON的;字符集= UTF-8」,'並以'數據:FORMDATA,'你模型將被正確綁定。 –