溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用ajax接收后臺(tái)發(fā)送過(guò)來(lái)的json數(shù)據(jù)

發(fā)布時(shí)間:2021-05-17 16:53:14 來(lái)源:億速云 閱讀:224 作者:Leah 欄目:web開發(fā)

如何使用ajax接收后臺(tái)發(fā)送過(guò)來(lái)的json數(shù)據(jù)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

后臺(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類

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格式類(數(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();
		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頁(yè)面代碼

<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="循環(huán)" id="btn"/>
</body>
</html>

什么是ajax

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)有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI