欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用ajax接收后台发送过来的json数据方法

程序员文章站 2022-03-04 16:56:03
今天给大家带来一个简单的使用ajax接收后台返回json格式的demo 废话不多说直接上代码 后台代码 package com.sidan.outjson;...

今天给大家带来一个简单的使用ajax接收后台返回json格式的demo

废话不多说直接上代码

后台代码

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>

写这个demo也费了很大劲,毕竟是新手,也从错误中学到了很多,比如datatype写成datatype时ajax默认接收的数据是为text这样导致我总是循环不了,还有就是json数据的格式是非常严谨的

就因为最后多了一个逗号就导致总是出错,并且循环不了,最后将错误打印出来才知道这个格式是错了的

以上这篇使用ajax接收后台发送过来的json数据方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

相关标签: ajax 接收 json