2

这是我的 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;
}
4

1 回答 1

1

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

success: function(result){
    if(result.Success){
        //code for success
    }else{
        alert(result.Error);
    }
}

类似的东西。希望这可以帮助

于 2013-11-15T04:02:09.967 回答