Failure Function is getting callback even after getting response from server

1.1k views Asked by At

I have a form which contains two textfields username and password when I submit the form my servlet is
getting called successfully and sending reponse properly. But in submit method failure is getting called.

Here is my code.

                                    form.submit({
                                                   success: function(form, action) {
                                                       console.log(action);
                                                       alert("Success");
                                                   },
                                                    failure: function(form, action) {
                                                        console.log(action);
                                                        alert("fail");
                                                    }

                                               });

My Servlet code.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 response.setContentType("text/html");
 PrintWriter out=response.getWriter();
 JSONObject obj=new JSONObject();
 try {
    obj.put("sucess",true);
    out.print(obj.toString());
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}

But if i have replaced form.submit with Ext.Ajax success function is getting called successfully.

 Ext.Ajax.request({
                                                   url:"login",
                                                    success: function(result, request) {
                                                        alert("success");
                                                       },
                                                        failure:function(response){
                                                            alert("failure");
                                                         console.log(response);
                                                        }
                                                  });

Why form.submit not working properly ??

Note:I am not doing any authentication at server side my intention is to check whether I am able to get json response from server,For Ext.Ajax.Request I am not sending params.

2

There are 2 answers

0
Surya Prakash Tumma On BEST ANSWER

Found the issue

I have done a mistake from my end,Before going to my issue I just want to explain about something form.submit(success,failure) will work on servers json response that is on success key but Ajax.request uses the request's response code and not success: true/false to determine which handler to call.

In below code while building json object I made a spelling mistake in success

try {
obj.put("sucess",true);
out.print(obj.toString());
} 

After correcting sucess to success my success function in form.submit is getting called successfully.

2
Stephen Tremaine On

Try checking what type of error to give you some direction in how to troubleshoot like:

failure: function(form, action) {
    switch (action.failureType) {
        case Ext.form.action.Action.CLIENT_INVALID:
            Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
            break;
        case Ext.form.action.Action.CONNECT_FAILURE:
            Ext.Msg.alert('Failure', 'Ajax communication failed');
            break;
        case Ext.form.action.Action.SERVER_INVALID:
           Ext.Msg.alert('Failure', action.result.msg);
   }
}