您好,登錄后才能下訂單哦!
如何使用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; /** * 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>
ajax是一種在無需重新加載整個網頁的情況下,能夠更新部分網頁的技術,可以通過在后臺與服務器進行少量數據交換,使網頁實現異步更新。
關于如何使用ajax接收后臺發送過來的json數據問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。