如何确定JSON从一个错误是成功的? [英] How does JSON determine a success from an error?
问题描述
我是新来的JSON,并使用它与ASP.NET MVC3已,但可以有人阐明如何每JSON结果?
I'm new to JSON and have been using it with MVC3 ASP.NET but could somebody shed some light on how to return an error per a JSON result?
我从查看下面的调用:
$.ajax({
type: "POST",
dataType: "json",
url: "EditJSON",
data: { FilmID: InputFilmID, Title: InputTitle, Description: InputDescription},
success: function (result) {
alert(result.Message + " updating film " + result.Title);
window.location = "../All";
},
error: function (error) {
alert('error');
}
});
控制器处理该请求是成功的。我会传回一个JSON错误,以便错误:函数处理回到了视图
Controller handles the request as a success. What would I pass back for a JSON error so that the error: function handled back at the View?
[AcceptVerbs("POST")]
public JsonResult EditJSON(BobsMoviesMVC2.Models.Film film)
{
filmRepository.Update(film);
return Json(new {Message = "Success", Title = film.Title });
// What would I return for an error here?
}
谢谢!
推荐答案
jQuery使用HTTP响应code到决定成败。
jQuery uses the HTTP response code to determine success or failure.
HTTP响应codeS> = 400被认为是错误的。 HTTP响应codeS> = 200和< 400被认为是成功的。
HTTP response codes >= 400 are considered errors. HTTP response codes >= 200 and < 400 are considered successes.
返回相应的HTTP codeS由服务器端code让你以后的行为。
Return appropriate HTTP codes from your server-side code to get the behavior you're after.
这篇关于如何确定JSON从一个错误是成功的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!