如何使用ajax接收后臺(tái)發(fā)送過(guò)來(lái)的json數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
創(chuàng)新互聯(lián)建站10多年成都定制網(wǎng)站服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁(yè)設(shè)計(jì)及高端網(wǎng)站定制服務(wù),成都定制網(wǎng)站及推廣,對(duì)小攪拌車(chē)等多個(gè)方面擁有多年的營(yíng)銷(xiāo)推廣經(jīng)驗(yàn)的網(wǎng)站建設(shè)公司。
后臺(tái)代碼
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類(lèi)
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; } }
將數(shù)據(jù)包裝成json格式類(lèi)(數(shù)據(jù)直接是循環(huán)添加的所以一樣這里是為了簡(jiǎn)單)
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(); ArrayListarr = 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 initArray(){ ArrayList arr = new ArrayList (); 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頁(yè)面代碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>Insert title here
ajax是一種在無(wú)需重新加載整個(gè)網(wǎng)頁(yè)的情況下,能夠更新部分網(wǎng)頁(yè)的技術(shù),可以通過(guò)在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新。
關(guān)于如何使用ajax接收后臺(tái)發(fā)送過(guò)來(lái)的json數(shù)據(jù)問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。