我有一些由 Ajax 呼叫的 Struts 2 動作。我嘗試處理錯誤并捕獲錯誤,但它沒有按預期作業。
方法ajax方法:
deleteIndicateur(id) {
$.ajax({
type: "DELETE",
url:"<%=request.getContextPath()%>/mesures.ce.delete.action?id=" id,
success: function (resp) {
alert(resp);
},
error: function (resp) {
alert(resp);
}
})
},
這Action:
@Action(value = "ce.delete", results = {
@Result(name = "success", type = "json", params = {"root", "indicateurDictionnaire"}),
@Result(name = "error", type = "json", params = {"root", "errors"})
})
public String delete() {
Integer dicId = Integer.valueOf(this.getParameter("id"));
try {
dicBo.delete(dicId);
} catch (Exception e) {
this.errors = getText(e.getMessage());
return ERROR;
}
return SUCCESS;
}
洗掉作業正常,但是當方法有錯誤時,錯誤被 ajax 請求中的成功函式捕獲。我必須如何將可捕獲的錯誤回傳給 Ajax?
uj5u.com熱心網友回復:
error如果回應具有與錯誤對應的狀態代碼,則呼叫回呼。請參閱HTTP 狀態代碼串列。
要回傳帶有錯誤訊息的回應并設定與錯誤對應的狀態碼,需要配置操作結果error。如同
@Result(name = "error", type = "json", params = {"root", "errors", "statusCode", 400})
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/419776.html
標籤:
