处理"成功"通过ajax调用从servlet返回的字符串

时间:2015-06-11 08:57:26

标签: javascript ajax servlets extjs

我有一个servlet接受我的ajax请求,它在我的ExtJs项目中从我的LoginView传递用户名和密码作为参数。这是相同的代码:

public class UserLoginController  extends MultiActionController {
OrgTreeService orgTreeService;

ModelAndView mv;
public String getLoginStatus(HttpServletRequest paramHttpServletRequest, HttpServletResponse paramHttpServletResponse)
        throws Exception{
    String str1 = paramHttpServletRequest.getParameter("user");
    String str2 = paramHttpServletRequest.getParameter("password");
    String s="";
      System.out.println("---node"+str1);
      System.out.println("---node"+str2);
      if(str1.equals("admin") && str2.equals("admin")){
          s="success";
      }else{
          s="failure";
      }


      return s;
}

这就是我从我的视图中发出ajax请求的方式:

doLoginClicked : function() {
    console.log("button pressed this time");
    var name = this.view.getUsername();
    var pass = this.view.getPassword();
    console.log(name);
    console.log(pass);

    Ext.Ajax.request({
        url: 'login/getLoginStatus.action',
        method:'post',
        params: {
            user: name,
            password: pass
            }
    ,
            success: function(response) {


                console.log(response);



                if (response=="success") { 

                  //  login.close(); 


                } else {
                    console.log("inside failure");


                    Ext.Msg.show({
                        title:'Fail!',
                        msg: response, 
                        icon: Ext.Msg.ERROR,
                        buttons: Ext.Msg.OK

                    });
                }
            },
            failure: function(response) {
                console.log("inside failure");

                  Ext.Msg.show({
                        title:'Error!',
                        msg: response,
                        icon: Ext.Msg.ERROR,
                        buttons: Ext.Msg.OK
                    });
                }
        });
    }

它确实在servlet的控制台中显示了用户名和密码,但我无法接受"成功"来自servlet的响应并相应地采取行动。这是在浏览器控制台中显示的响应:

Object {request: Object, requestId: 1, status: 200, statusText: "OK", responseText: ""…}getAllResponseHeaders: () {arguments: nullcaller: nulllength: 0name: ""prototype: response.getAllResponseHeadersconstructor: () {arguments: nullcaller: nulllength: 0name: ""prototype: response.getAllResponseHeaders__proto__: Empty() {}<function scope>__proto__: Object__proto__: Empty() {}<function scope>getResponseHeader: (header) {request: Objectasync: truebinary: falseheaders: Objectid: 1options: Object__proto__: ObjectrequestId: 1responseText: ""responseXML: nullstatus: 200statusText: "OK"__proto__: Object

有没有办法让我可以在&#34;成功&#34;从servlet返回?

1 个答案:

答案 0 :(得分:0)

success: function(response)

上面代码行中的response是一个对象。您必须更深入地了解您的对象,例如response.status == '200'response.responseText == "success"

由于您没有给出确切的答案,我将无法提供确切的代码,但这就是问题所在。