这是我的javascript代码,我的控制器看起来像这样 但每当有错误或异常时,它总会成功回调我在这里缺少什么
var options = {
iframe: true,
type: 'post',
url: "api/Registration/Register",
data: { "data": jsonData },
dataType: 'json',
success: function (responseText, statusText, xhr, $form)
{
if (statusText == 'success') {
}
else { //code == 'FAIL'
}
},
error: function (response, status, err) {
}};
$('#registration').ajaxSubmit(options);
[HttpPost]
public Task<HttpResponseMessage> Register()
{
//create task object and read server data
try
{
var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(JsonConvert.SerializeObject(assistedRegResponse.Response))
};
return response;
}
catch (Exception ex){ throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));}
return task;
}
答案 0 :(得分:1)
在您发布的代码中,您始终将状态代码设置为ok。从这里Asp.Net MVC3, returning success JsonResult你应该根据控制器上的结果设置结果并返回它。
success: function(result){
if(result.Success){
//code for success
}else{
alert(result.Error);
}
}
这样的事情。希望这有帮助