解決ajax返回驗(yàn)證的時候總是彈出error錯誤的方法
發(fā)一個簡單案例:
前臺:
<%@ 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 , // 組件添加屬性:讓關(guān)閉按鈕消失 //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("進(jìn)來了"); }, 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()); } }
點(diǎn)擊登錄時:
解決辦法:彈出error信息一般有兩種可能:
第一種:url錯誤,后臺直接得不到值
可以用火狐的firebug查看:如果響應(yīng)了信息,則不是這個問題,那么就有可能是第二種情況:
返回?cái)?shù)據(jù)類型錯誤:
在我這個例子中,返回的數(shù)據(jù)無意中打印了兩次,這兩句刪去一句就好了:
out.print(json.toString()); response.getWriter().write(json.toString());
造成了錯誤。這時在firebug顯示的信息是:
以上就是為大家分析的用ajax返回驗(yàn)證的時候總是彈出error的原因,希望對大家解決此類問題有所幫助。
相關(guān)文章
AJAX POST數(shù)據(jù)中有特殊符號(轉(zhuǎn)義字符)導(dǎo)致數(shù)據(jù)丟失的解決方法
這篇文章主要介紹了Ajax發(fā)送轉(zhuǎn)義字符 、>、<、"接收數(shù)據(jù)不全問題,需要的朋友可以參考下2023-06-06AJAX在不同瀏覽器中XMLHttpRequest對象的生成示例
本文為大家介紹下AJAX在不同瀏覽器中XMLHttpRequest對象是怎樣生成的,以方便我們針對不同瀏覽器進(jìn)行測試,感興趣的朋友可以參考下,希望對大家有所幫助2013-07-07Ajax與JSON的一些學(xué)習(xí)總結(jié)
Ajax技術(shù)的核心是XMLHttpRequest對象(簡稱XHR),可以通過使用XHR對象獲取到服務(wù)器的數(shù)據(jù),然后再通過DOM將數(shù)據(jù)插入到頁面中呈現(xiàn)。雖然名字中包含XML,但Ajax通訊與數(shù)據(jù)格式無關(guān),所以我們的數(shù)據(jù)格式可以是XML或JSON等格式2012-08-08Ajax調(diào)用restful接口傳送Json格式數(shù)據(jù)的方法
這篇文章主要介紹了Ajax調(diào)用restful接口傳送Json格式數(shù)據(jù)的方法的相關(guān)資料,非常不錯,具有參考借鑒價(jià)值,感興趣的朋友一起看下吧2016-07-07仿google搜索提示 SuggestFramework的使用
使用幫助(英文版翻譯而來,可能有錯誤,請大家仔細(xì)核對,也希望對新手理解能有所幫助)2008-09-09Ajax實(shí)現(xiàn)帶有驗(yàn)證碼的局部刷新登錄界面
很多登錄界面都有驗(yàn)證碼功能局部刷新效果,接下來通過本文給大家分享ajax實(shí)現(xiàn)帶有驗(yàn)證碼的局部刷新登錄界面,感興趣的朋友一起看看吧2016-09-09AJAX+JSP實(shí)現(xiàn)讀取XML內(nèi)容并按排列顯示輸出的方法示例
這篇文章主要介紹了AJAX+JSP實(shí)現(xiàn)讀取XML內(nèi)容并按排列顯示輸出的方法,結(jié)合實(shí)例形式分析了ajax與后臺jsp頁面交互實(shí)現(xiàn)xml內(nèi)容的排列輸出相關(guān)操作技巧,需要的朋友可以參考下2018-06-06