服务器错误总是从MVC HttpPost调用ajax成功回调jQuery

时间:2013-11-15 03:23:10

标签: ajax asp.net-mvc

这是我的javascript代码,我的控制器看起来像这样 但每当有错误或异常时,它总会成功回调我在这里缺少什么

的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;
}

1 个答案:

答案 0 :(得分:1)

在您发布的代码中,您始终将状态代码设置为ok。从这里Asp.Net MVC3, returning success JsonResult你应该根据控制器上的结果设置结果并返回它。

success: function(result){
    if(result.Success){
        //code for success
    }else{
        alert(result.Error);
    }
}
这样的事情。希望这有帮助