如何使用ajax接收后台发送过来的json数据?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
后台代码
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;@WebServlet("/OutJson")public class OutJson extends HttpServlet {private static final long serialVersionUID = 1L; public OutJson() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request,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>
什么是ajax
ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术,可以通过在后台与服务器进行少量数据交换,使网页实现异步更新。
关于如何使用ajax接收后台发送过来的json数据问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。