欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

解决ajax返回验证的时候总是弹出error错误的方法

程序员文章站 2022-07-02 18:20:14
发一个简单案例: 前台: <%@ page language="java" import="java.util.*" pageencoding="ut...

发一个简单案例:
前台:

<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> 
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"> 
<html> 
 <head> 
   <title>用户登录</title> 
   <script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.min.js"></script> 
   <script type="text/javascript" src="../js/jquery-easyui-1.3.5/jquery.easyui.min.js"></script> 
   <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/default/easyui.css" type="text/css"></link> 
   <link rel="stylesheet" href="../js/jquery-easyui-1.3.5/themes/icon.css" type="text/css"></link> 
   <script type="text/javascript" src="../js/jquery-easyui-1.3.5/locale/easyui-lang-zh_cn.js"></script> 
   <meta http-equiv="content-type" content="text/html;charset=utf-8" /> 
   <script type = "text/javascript" charset = "utf-8"> 
   $(function(){ 
     var logindialog; 
     logindialog = $('#logindialog').dialog({ 
       closable : false , // 组件添加属性:让关闭按钮消失 
       //modal : true, //模式化窗口 
       buttons : [{ 
         text:'注册', 
         handler:function(){ 
            
         } 
       }, 
       { 
         text:'登录', 
         handler:function(){ 
            $.ajax({ 
             url:'../servlet/login_do', 
             data :{ 
                name:$('#loginform input[name=name]').val(), 
                password:$('#loginform input[name=password]').val() 
               }, 
             datatype:'json', 
             success:function(r){ 
              //var dataobj=eval("("+data+")"); 
               alert("进来了"); 
             }, 
             error:function(){ 
               alert("失败"); 
             }   
              
           }); 
            //alert(data) 
         } 
       }] 
     }); 
   }); 
   </script>  
 </head> 
 <body style=”width:100%;height:100%;" > 
    <div id = "logindialog" title = "用户登录" style = "width:250px;height:250px;" > 
      <form id = "loginform" method = "post"> 
        <table> 
        <tr> 
          <th>用户名 :</th> 
          <td><input type = "text" class = "easyui-validatebox" data-options="required:true" name = "name"><br></td> 
        </tr> 
        <tr> 
          <th>密码: </th> 
          <td> <input type = "password" class = "easyui-validatebox" data-options="required:true" name = "password"><br></td></td> 
        </tr> 
        </table> 
      </form>  
    </div> 
 </body> 
</html> 

 后台:

public class login_do extends httpservlet { 
  public void doget(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
      this.dopost(request, response); 
  } 
  public void dopost(httpservletrequest request, httpservletresponse response) 
      throws servletexception, ioexception { 
    request.setcharacterencoding("utf-8");  
    response.setcharacterencoding("utf-8"); 
    string name =request.getparameter("name"); 
    string password = request.getparameter("password"); 
    string js = "{\"name\":name,\"password\":password}"; 
    printwriter out = response.getwriter(); 
    jsonobject json = new jsonobject(); 
    json.put("name",name); 
    out.print(json.tostring()); 
    response.getwriter().write(json.tostring()); 
  } 
}

 点击登录时:

解决ajax返回验证的时候总是弹出error错误的方法

解决办法:弹出error信息一般有两种可能:
第一种:url错误,后台直接得不到值
可以用火狐的firebug查看:如果响应了信息,则不是这个问题,那么就有可能是第二种情况
返回数据类型错误:
在我这个例子中,返回的数据无意中打印了两次,这两句删去一句就好了:

out.print(json.tostring()); 
response.getwriter().write(json.tostring());  

造成了错误。这时在firebug显示的信息是:

解决ajax返回验证的时候总是弹出error错误的方法

解决ajax返回验证的时候总是弹出error错误的方法

以上就是为大家分析的用ajax返回验证的时候总是弹出error的原因,希望对大家解决此类问题有所帮助。