【问题标题】:Failure Function is getting callback even after getting response from server即使在从服务器获得响应后,失败函数也会得到回调
【发布时间】:2014-11-23 18:15:27
【问题描述】:

当我提交我的 servlet 的表单时,我有一个包含两个文本字段用户名和密码的表单
成功调用并正确发送响应。但是在提交方法失败被调用。

这是我的代码。

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

                                               });

我的 Servlet 代码。

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();
}

}

但是如果我用 Ext.Ajax 替换了 form.submit,那么成功函数就会被成功调用。

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

为什么 form.submit 不能正常工作??

注意:我没有在服务器端进行任何身份验证,我的目的是检查我是否能够从服务器获得 json 响应,对于 Ext.Ajax.Request 我没有发送参数。

【问题讨论】:

    标签: ajax extjs extjs4 extjs4.1 extjs3


    【解决方案1】:

    尝试检查什么类型的错误,以便为您提供一些解决问题的方向,例如:

    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);
       }
    }
    

    【讨论】:

    • 是的,格式相同,正如我所说,它适用于 ext.ajax 请求
    • 抱歉 - 意思是发表评论,而不是回答 - 您没有使用 standardSubmit: true 配置表单吗?否则,可能会解析并在发回的操作对象中找到响应 - 用有关如何执行此操作的详细信息更新我的答案。
    【解决方案2】:

    发现问题

    我从头到尾都犯了一个错误,在讨论我的问题之前,我只想解释一下 form.submit(success,failure) 将在服务器 json 响应上工作,该响应是成功键,但 Ajax.request 使用请求的响应代码而不是 success: true/false 来确定调用哪个处理程序。

    在下面的代码中,在构建 json 对象时,我成功犯了一个拼写错误

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

    在将成功更正为成功后,我在 form.submit 中的成功函数被成功调用。

    【讨论】:

      猜你喜欢
      • 2023-02-01
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多