今天給大家?guī)?lái)一個(gè)簡(jiǎn)單的使用ajax接收后臺(tái)返回json格式的demo
廢話不多說(shuō)直接上代碼
后臺(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();
ArrayListPerson> 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 ArrayListPerson> initArray(){
ArrayListPerson> arr = new ArrayListPerson>();
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>
寫(xiě)這個(gè)demo也費(fèi)了很大勁,畢竟是新手,也從錯(cuò)誤中學(xué)到了很多,比如dataType寫(xiě)成datatype時(shí)ajax默認(rèn)接收的數(shù)據(jù)是為text這樣導(dǎo)致我總是循環(huán)不了,還有就是json數(shù)據(jù)的格式是非常嚴(yán)謹(jǐn)?shù)?/p>
就因?yàn)樽詈蠖嗔艘粋€(gè)逗號(hào)就導(dǎo)致總是出錯(cuò),并且循環(huán)不了,最后將錯(cuò)誤打印出來(lái)才知道這個(gè)格式是錯(cuò)了的
以上這篇使用ajax接收后臺(tái)發(fā)送過(guò)來(lái)的json數(shù)據(jù)方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- 利用ajax傳遞數(shù)組及后臺(tái)接收的方法詳解
- jQuery使用ajax方法解析返回的json數(shù)據(jù)功能示例
- jquery用ajax方式從后臺(tái)獲取json數(shù)據(jù)后如何將內(nèi)容填充到下拉列表
- 淺析Ajax后臺(tái)success傳來(lái)json數(shù)據(jù)的問(wèn)題