发一个简单案例:
前台:
<%@ 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错误,后台直接得不到值
可以用火狐的firebug查看:如果响应了信息,则不是这个问题,那么就有可能是第二种情况:
返回数据类型错误:
在我这个例子中,返回的数据无意中打印了两次,这两句删去一句就好了:
out.print(json.toString()); response.getWriter().write(json.toString());
造成了错误。这时在firebug显示的信息是:
以上就是为大家分析的用ajax返回验证的时候总是弹出error的原因,希望对大家解决此类问题有所帮助。
本文转载自微信公众号「Linux开发那些事儿」,作者 LinuxThings 。转载本文请联...
简介 “ 大家好我是帅哥欢迎来到帅哥的程序人生我会把经历分享出来助你了解圈内...
不少Windows 10用户之前都抱怨一个问题,那就是系统的屏幕出现了渲染问题,而微...
一、Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候...
前言 aop即是面向切面编程,众多Aop框架里Castle是最为人所知的,另外还有死去的...
console.log ,作为一个前端开发者,可能每天都会用它来分析调试,但这个简单函...
2月26日消息 众所周知,Windows 10 的安全更新和其他重要累计更新通常是在同一天...
继 Australis 和 Photon 之后,Mozilla 现又酝酿为 Firefox 带来名为Proton的全...
开发过程中,我们经常会遇到代码回滚的情况。正常人都知道,git 回滚有两大宝: ...
互联网业务往往使用MySQL数据库作为后台存储,存储引擎使用InnoDB。我们针对互联...