使用jQuery.ajax时遇到麻烦

时间:2013-07-01 04:57:47

标签: jquery

虽然它有效,但我仍然坚持使用Jqueryajax,但它并没有检查我所有的条件。 这是我使用jQueryAjax的代码:

jQuery.ajax({
    type: "POST",
    //data: 'user= ' + user + ' &pwd= ' + pwd,
    url: "jsp/admin/master/Connectteacher.jsp?tuser="+tuser+"&tpwd="+tpwd,
    async: false,
    cache: false,
    success: function( check ) {

       if ( $.trim( check ) == 'new' ) {
           window.location.replace("jsp/admin/master/Newuser.jsp?name="+tuser);
       }
       if ( $.trim( check ) == 'proceed') {  
           window.location.replace("jsp/admin/master/teacher-profile.jsp");
       }
       if ( $.trim( check ) == 'block') {
           $('#teacherError').html("You are blocked!");
       }
       if ( $.trim( check ) == 'incorrect') {
           $('#teacherError').html("Incorrect username or password");
       }

    }, error: function() {        
       $('#teacherError').html("database not connected");
    }

});

这是我的Connectteacher.jsp:

LoginTeacher l = new LoginTeacher();
String i = l.teacherAuthentication(request.getParameter("tuser"), request.getParameter("tpwd"));

char chusr = i.charAt(1);
char atmpts = i.charAt(0);
int at = Character.getNumericValue( atmpts );
System.out.println("matters" + at);

if( chusr == 'T' && at < 4 ) {
   out.print("new");
}
if( chusr == 'F' && at < 4 ) {
   System.out.println("inside old");
   out.print("proceed");
}
if( at > 3 ) {
  out.print("block");
}
//if(chusr==' ')
//{
  //out.print("incorrect");
//}

它适用于前3个if条件,但不适用于我包含第4个条件。是否存在任何限制,我们只能在jQueryAjax中使用有限的条件。请帮帮我

3 个答案:

答案 0 :(得分:0)

System.out.println("matters"+at);行也会出现在您的输出中,无法满足您的任何条件。

此外,使用像Firebug这样的东西可以帮助您准确确定服务器发回的内容。

答案 1 :(得分:0)

尝试jquery代码。我认为它会工作..我现在不在jsp中,你需要使用任何数据交换格式对输出数据进行编码,例如json,html或任何其他

  $.ajax({
    type: "POST",
    data: {"user": user ,"pwd": pwd },
    url: "jsp/admin/master/Connectteacher.jsp,
    cache: false,
    success: function(check) {  

        if (check == "new") {
            window.location.replace("jsp/admin/master/Newuser.jsp?name="+tuser);
          }
       if (check == "proceed") {  
           window.location.replace("jsp/admin/master/teacher-profile.jsp");
       }
       if (check == "block") {
                $('#teacherError').html("You are blocked!");
                 }
       if (check == "incorrect") {
                $('#teacherError').html("Incorrect username or password");
                  }
        },
    error: function() {        
       //Statements for handling ajax errors (eg . checking request response)               
}

});

答案 2 :(得分:0)

如果要检查字符串是否为空,或者检查方法

下方的检查是否为空

方法1:

if (chusr != null && chusr != '') {
// Your Code Here
}

方法2:

if (chusr.length != 0 && chusr != '') {
// Your Code Here
}