解決ajax返回驗證的時候總是彈出error錯誤的方法_AJAX相關

來源:互聯網
上載者:User

發一個簡單案例:
前台:

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

 點擊登入時:

解決辦法:彈出error資訊一般有兩種可能:
第一種:url錯誤,後台直接得不到值
可以用Firefox的firebug查看:如果響應了資訊,則不是這個問題,那麼就有可能是第二種情況
返回資料類型錯誤:
在我這個例子中,返回的資料無意中列印了兩次,這兩句刪去一句就好了:

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

造成了錯誤。這時在firebug顯示的資訊是:

以上就是為大家分析的用ajax返回驗證的時候總是彈出error的原因,希望對大家解決此類問題有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.