我试图建立我的控制器,这样他们可以使用HTTP错误代码来响应发送到AJAX调用。因此,例如,我有我的登录Ajax调用控制器动作:设置.NET MVC HTTP阿贾克斯错误代码调用
[System.Web.Mvc.HttpPost, System.Web.Mvc.AllowAnonymous]
public ActionResult Login(string userName, string password, bool rememberMe, string returnUrl)
{
[...]
var loginError = new HttpResponseMessage(HttpStatusCode.Unauthorized)
{
Content = new StringContent("Lorem ipsum 2 " + ErrorMessages.LOGINERROR),
ReasonPhrase = "Lorem ipsum 2 " + ErrorMessages.LOGINERROR
};
throw new HttpResponseException(loginError);
}
$.ajax({
type: "POST",
url: url,
data: data,
dataType: "text",
success: callBack,
error: function() { console.log("Error..."); },
statusCode : {
401: function (result) {
console.log("Login failed (401): " + result);
}
}
});
我想有几件事情我没有做对的,如果有人能指出来,这将是可爱的!
谢谢!
感谢这帮了我很多。但是,代码中存在一些问题,我对其进行了修正。 – NaveenBhat 2013-09-05 11:12:46