发一个简单案例:
前台:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>用户登录
后台:
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的原因,希望对大家解决此类问题有所帮助。