如何调用$阿贾克斯误差函数用C#MVC4? [英] How to call error function in $.ajax with c# MVC4?
问题描述
我在MVC4与C#项目。在这个项目中,我的控制器中的一个有一个方法由一个Ajax函数被调用:
I have a project in MVC4 with C#. In this project, one of my controllers has a method to be called by an Ajax function:
[HttpPost]
public string EditPackage(int id, string newPkgName)
{
try{
//do logic here
return "OK";
}catch(Exception exc){
return "An error occurred, please try later! " + exc.Message;
}
}
此方法由下列Ajax函数调用,使用jQuery
This method is called by the following Ajax functions, using jQuery:
$.ajax({
url: $(this).data('url'),
type: 'POST',
contentType: 'application/json; charset=utf-8',
traditional: true,
data: JSON.stringify({ id: id, newPkgName: newPkgName}),
success: function () {
location.reload(true);
successNotification("Package edited successfuly!");
},
error: function (message) {
errorNotification(message);
}
});
本code的问题是,即使服务器返回返回时发生错误,请稍后再试!+ exc.Message;
信息在catch,成功的功能总是叫之一。
The problem with this code, is that even if the server returns the return "An error occurred, please try later! " + exc.Message;
message in the catch, the success function is the one always called.
为了话,我从来不管我做什么运行错误的功能。
In order words, I never run the error function no matter what I do.
要解决这个问题我查了一下官方文档:
To fix this I checked the official documentation:
- http://api.jquery.com/jQuery.ajax/
不过,由于我failry新来这个我不明白任何参数,也不怎么有效地使用它们。
However, since I am failry new to this I can't understand any of the parameters, nor how to use them effectively.
我怎样才能创造一个良好的错误消息,使用Ajax,jQuery和我的控制器?
How can I create a good error message with all the possible information using Ajax, jQuery and my controller?
推荐答案
的 $。阿贾克斯
错误部分>只调用火灾如果返回的状态code是以外的任何其他 200 OK
。在你的情况你是返回一个明文响应,因此这将是 200
。你可以改变这样的这种行为:
The error
part of the $.ajax
call only fires if the returned status code is anything other than 200 OK
. In your case you are returning a plaintext response which will therefore be 200
. You can change this behaviour like this:
try {
// do logic here
return "OK";
}
catch (Exception exc) {
return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Bad Request");
}
error: function (jqXHR, textStatus, errorThrown) {
errorNotification(textStatus);
}
您可以修改的HTTPStatus code
来任何适合您的需要。
You can change the HttpStatusCode
to whatever suits your need.
另外,你可以保持 200
响应,并用标志返回JSON指示请求是否成功:
Alternatively, you can keep the 200
response and return JSON with a flag to indicate whether or not the request was successful:
[HttpPost]
public ActionResult EditPackage(int id, string newPkgName)
{
try {
//do logic here
return Json(new { Success = true, Message = "OK"});
}
catch (Exception exc) {
return Json(new { Success = false, Message = "An error occurred, please try later! " + exc.Message });
}
}
然后就可以删除错误
处理,并检查标志的状态在成功
处理程序:
Then you can remove the error
handler, and check the state of the flag in your success
handler:
success: function(response) {
if (response.Success) {
location.reload(true);
successNotification("Package edited successfuly!");
}
else {
errorNotification(response.Message);
}
},
这篇关于如何调用$阿贾克斯误差函数用C#MVC4?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!