Ersch
Ersch

Reputation: 315

How to return catchable error to the ajax call in Struts 2?

I have some Struts 2 actions called by Ajax. I try to deal with error and catch the error but it doesn't work as expected.

the method ajax method:

deleteIndicateur(id) {
   $.ajax({
   type: "DELETE",
   url:"<%=request.getContextPath()%>/mesures.ce.delete.action?id=" + id,
   success: function (resp) {
    alert(resp);
   },
   error: function (resp) {
    alert(resp);
   }
 })
},

and this 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;
        }

Delete works fine but when the method has an error, the error is catch by the success function in the ajax request. How I must do to return a catchable error to the Ajax?

Upvotes: 1

Views: 374

Answers (1)

Roman C
Roman C

Reputation: 1

The callback error is called if the response has a status code corresponding to the error. See the list of HTTP status codes.

To return a response with error messages and setting the status code corresponding to the error needs to configure the action result error. Similar to

@Result(name = "error", type = "json", params = {"root", "errors", "statusCode", 400})
        })

Upvotes: 1

Related Questions