The method to solve the error error always pops up when Ajax returns verification

Time:2020-10-22

Send a simple case:
Reception:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
 <head> 
   < title > user login 
   <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, // component add attribute: let close button disappear 
       //Modal: true, // modal window 
       buttons : [{ 
         Text: 'register', 
         handler:function(){ 
            
         } 
       }, 
       { 
         Text: 'Login', 
         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 ("come in"); 
             }, 
             error:function(){ 
               Alert ("failed"); 
             }   
              
           }); 
            //alert(data) 
         } 
       }] 
     }); 
   }); 
   </script>  
 </head> 
 <body style=”width:100%;height:100%;" > 
    < div id = logindialog "title = user login" style =“ width:250px;height :250px;" > 
      <form id = "loginForm" method = "post"> 
        <table> 
        <tr> 
          < th > User Name: < / th > 
          <td><input type = "text" class = "easyui-validatebox" data-options="required:true" name = "name"><br></td> 
        </tr> 
        <tr> 
          < th > password: < / th > 
          <td> <input type = "password" class = "easyui-validatebox" data-options="required:true" name = "password"><br></td></td> 
        </tr> 
        </table> 
      </form>  
    </div> 
 </body> 
</html>

Backstage:


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

When clicking login:

terms of settlement:There are two possibilities to pop up the error message:
First:URL error, the background can not get the value directly
You can see it in firebug: if you respond to a message, it’s not the problem, it’s probablyThe second situation
Return data type error:
In my example, the returned data is accidentally printed twice. Just delete one of these two sentences:


out.print(json.toString()); 
response.getWriter().write(json.toString());  

It made a mistake. The message displayed in firebug is:

The above is the reason why errors always pop up when you use ajax to return verification. I hope it can help you solve such problems.