From an ajax function, I call an MVC controller method and return a -1 if the method fails. How do I code the ajax function to receive the -1 so that I can execute a javascript alert?
从ajax函数,我调用MVC控制器方法,如果方法失败,则返回-1。如何编写ajax函数来接收-1以便我可以执行javascript警报?
function RejectButtonProcess()
{
// This will execute the method in the specified controller.
$.ajax({
type: "POST",
url: '@Url.Action("NHPDAdminRejectAddress", "Home")',
error: function(XMLHttpRequest, textStatus, errorThrown)
{
//alert("Status: " + textStatus); alert("Error: " + errorThrown);
alert("An error occurred during the reject process. Contact the IT department.");
}
});
}
3 个解决方案
#1
1
Add the success callback:
添加成功回调:
success: function (data, textStatus, jqXHR) {
// process the data
},
The data
variable coming back will contain the return code (probably in JSON, depending on how you set up your endpoint).
返回的数据变量将包含返回代码(可能是JSON,具体取决于您设置端点的方式)。
#2
0
You can do it as follows,
你可以这样做,
$.ajax({
type: "POST",
url: '@Url.Action("NHPDAdminRejectAddress", "Home")',
data: JSON.stringify(model), \\ Pass data if any
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if(result == -1) { \\Check Result
alert("Error");
}
},
error: function () {
}
});
#3
0
Although sending back a response and checking the data object in the callback (as explained above by @Richard and @Jatin) will work, I tend to follow the following pattern of rolling my own AjaxResult object, hydrating this object with the model state validity, the success state, as well as any data I need to pass back to the $.ajax success callback function. This enables a flexible and elegant solution in handling $.ajax requests. For example:
虽然回送响应并检查回调中的数据对象(如上所述@Richard和@Jatin)会起作用,但我倾向于按照以下模式滚动我自己的AjaxResult对象,用模型状态有效性来保护这个对象,成功状态,以及我需要传递回$ .ajax成功回调函数的任何数据。这为处理$ .ajax请求提供了灵活而优雅的解决方案。例如:
// custom ajax request result class
public class AjaxResult
{
public bool IsValid { get; set; }
public bool Success { get; set; }
public string Message { get; set; }
public Dictionary<string, object> Data { get; set; }
public AjaxResult()
{
Data = new Dictionary<string, object>();
}
public void AddData(string dataKey, object dataValue)
{
if (dataKey != null && !Data.ContainsKey(dataKey))
{
Data.Add(dataKey, dataValue);
}
}
}
// controller
public ActionResult NhpdAdminRejectAddress(NhpdAdminViewModel model)
{
AjaxResult result = new AjaxResult();
if (ModelState.IsValid)
{
// do something
result.AddData("Code", -1);
result.IsValid = true;
result.Success = true;
}
return Json(result);
}
// ajax post request
$.ajax({
type: "POST",
url: '@Url.Action("NHPDAdminRejectAddress", "Home")',
data: JSON.stringify(model),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result != null && result.Data != null && result.Data.Code != null)
{
int code = result.Data.Code;
}
},
error: function () {
// handle error
}
});
#1
1
Add the success callback:
添加成功回调:
success: function (data, textStatus, jqXHR) {
// process the data
},
The data
variable coming back will contain the return code (probably in JSON, depending on how you set up your endpoint).
返回的数据变量将包含返回代码(可能是JSON,具体取决于您设置端点的方式)。
#2
0
You can do it as follows,
你可以这样做,
$.ajax({
type: "POST",
url: '@Url.Action("NHPDAdminRejectAddress", "Home")',
data: JSON.stringify(model), \\ Pass data if any
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if(result == -1) { \\Check Result
alert("Error");
}
},
error: function () {
}
});
#3
0
Although sending back a response and checking the data object in the callback (as explained above by @Richard and @Jatin) will work, I tend to follow the following pattern of rolling my own AjaxResult object, hydrating this object with the model state validity, the success state, as well as any data I need to pass back to the $.ajax success callback function. This enables a flexible and elegant solution in handling $.ajax requests. For example:
虽然回送响应并检查回调中的数据对象(如上所述@Richard和@Jatin)会起作用,但我倾向于按照以下模式滚动我自己的AjaxResult对象,用模型状态有效性来保护这个对象,成功状态,以及我需要传递回$ .ajax成功回调函数的任何数据。这为处理$ .ajax请求提供了灵活而优雅的解决方案。例如:
// custom ajax request result class
public class AjaxResult
{
public bool IsValid { get; set; }
public bool Success { get; set; }
public string Message { get; set; }
public Dictionary<string, object> Data { get; set; }
public AjaxResult()
{
Data = new Dictionary<string, object>();
}
public void AddData(string dataKey, object dataValue)
{
if (dataKey != null && !Data.ContainsKey(dataKey))
{
Data.Add(dataKey, dataValue);
}
}
}
// controller
public ActionResult NhpdAdminRejectAddress(NhpdAdminViewModel model)
{
AjaxResult result = new AjaxResult();
if (ModelState.IsValid)
{
// do something
result.AddData("Code", -1);
result.IsValid = true;
result.Success = true;
}
return Json(result);
}
// ajax post request
$.ajax({
type: "POST",
url: '@Url.Action("NHPDAdminRejectAddress", "Home")',
data: JSON.stringify(model),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
if (result != null && result.Data != null && result.Data.Code != null)
{
int code = result.Data.Code;
}
},
error: function () {
// handle error
}
});