今天给大家带来一个简单的使用ajax接收后台返回json格式的demo
废话不多说直接上代码
后台代码
package com.sidan.outjson; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.sidan.jsonutil.GetJson; /** * Servlet implementation class OutJson */ @WebServlet("/OutJson") public class OutJson extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public OutJson() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); String s = GetJson.getJson(); out.print(s); } }
Pserson类
package com.sidan.jsonutil; public class Person { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
将数据包装成json格式类(数据直接是循环添加的所以一样这里是为了简单)
package com.sidan.jsonutil; import java.util.ArrayList; public class GetJson { public static String getJson(){ return json().toString(); } public static StringBuffer json(){ StringBuffer sb = new StringBuffer(); ArrayList<Person> arr = initArray(); int x = 0; sb.append("["); for(Person p:arr){ sb.append("{"); sb.append("\"name\""); sb.append(":"); sb.append("\""+p.getName()+"\""); sb.append(","); sb.append("\"age\""); sb.append(":"); sb.append("\""+p.getAge()+"\""); sb.append(","); sb.append("\"sex\""); sb.append(":"); sb.append("\""+p.getSex()+"\""); sb.append("}"); if(x != arr.size()-1){ sb.append(","); } x++; } sb.append("]"); return sb; } public static ArrayList<Person> initArray(){ ArrayList<Person> arr = new ArrayList<Person>(); for(int i = 0;i < 10;i++){ Person p = new Person(); p.setName("sdchen"); p.setAge(20); p.setSex("man"); arr.add(p); } return arr; } }
jsp页面代码
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> <script src="jQuery/jquery-3.1.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ var url = "OutJson"; $.ajax({ url:url, type:"post", dataType:"json", error:function(XMLHttpRequest, textStatus, errorThrown){ alert(XMLHttpRequest); alert(textStatus); alert(errorThrown); }, success:function(data){ $.each(data,function(idx,obj){ var li = document.createElement("li"); li.innerHTML = "<a>" + obj.name + "</a>"; document.getElementById("ul1").appendChild(li); }); } }); }); }); </script> </head> <body> <ul id="ul1"></ul> <input type="button" value="循环" id="btn"/> </body> </html>
写这个demo也费了很大劲,毕竟是新手,也从错误中学到了很多,比如dataType写成datatype时ajax默认接收的数据是为text这样导致我总是循环不了,还有就是json数据的格式是非常严谨的
就因为最后多了一个逗号就导致总是出错,并且循环不了,最后将错误打印出来才知道这个格式是错了的
以上这篇使用ajax接收后台发送过来的json数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持呐喊教程。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#nhooo.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。