Ajax即Asynchronous Javascript And XML(异步JavaScript和XML)在 2005年被Jesse James Garrett提出的新术语,用来描述一种使用现有技术集合的‘新’方法,包括: 或 XHTML, CSS, JavaScript, DOM, XML, XSLT, 以及最重要的XMLHttpRequest。 [3] 使用Ajax技术网页应用能够快速地将增量更新呈现在用户界面上,而不需要重载(刷新)整个页面,这使得程序能够更快地回应用户的操作。 [3]
$.ajax({
url: "",
type: "",
data: {
name: "yhc",
id: 5321
},
success: function(res) {
console.log(res)
}
})
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2022/8/20 0020
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
get/post方法
get 与post区别
$.get("xxx", {
name: "xxx",
id: 17
}, function(resText, status, xhr) {
console.log(resText)
})
$.post("xxx", {
name: "xxx",
id: 17
}, function(resText, status, xhr) {
console.log(resText)
})
在前后端数据传输交互中,经常会遇到字符串(String)与json,XML等格式相互转换与解析,其中json以跨语言,跨前后端的优点在开发中被频繁使用,基本上可以说是标准的数据交换格式。fastjson 是一个java语言编写的高性能且功能完善的JSON库,它采用一种“假定有序快速匹配”的算法,把JSON Parse 的性能提升到了极致。它的接口简单易用,已经被广泛使用在缓存序列化,协议交互,Web输出等各种应用场景中。
maven依赖:
com.alibaba
fastjson
版本根据自己需要
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static finalT parseObject(String text, Class clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final
List parseArray(String text, Class clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
package com.csi.eshop.controller.product;
import com.alibaba.fastjson.JSON;
import com.csi.eshop.domain.Product;
import com.csi.eshop.service.ProductService;
import com.csi.eshop.service.impl.ProductServiceImpl;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/JSON_EACHController")
public class JSON_EACHController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("application/json;charset=utf-8");
PrintWriter out = resp.getWriter();
ProductService productService = new ProductServiceImpl();
List list = productService.list();
String s = JSON.toJSONString(list);
out.println(s);
}
}
会输出集合以及数组对象
resp.setContentType("application/json;charset=utf-8");
使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据。
实际应用
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: 华为
Date: 2022/8/20
Time: 22:12
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
import com.alibaba.fastjson.JSON;
import domain.Grade;
import domain.Result;
import domain.Student;
import org.junit.Test;
import java.util.*;
public class test1 {
@Test
public void test1(){
Result result = new Result(200,"成功");
String jsonStr = JSON.toJSONString(result);
System.out.println(jsonStr);
}
@Test
public void test2(){
Student student = new Student();
student.setStudentNo("10001");
student.setBornDate(new Date());
Grade grade = new Grade();
grade.setGradeName("一年");
grade.setGradeId(1);
Set students = new HashSet<>();
students.add(student);
grade.setStudents(students);
student.setGrade(grade);
String jsonStr = JSON.toJSONString(student);
System.out.println(jsonStr);
}
@Test
public void test3(){
Grade grade = new Grade();
grade.setGradeName("一年");
grade.setGradeId(1);
Student student1 = new Student();
student1.setStudentNo("10001");
student1.setBornDate(new Date());
Student student2 = new Student();
student2.setStudentNo("10002");
student2.setBornDate(new Date());
Set students = new HashSet<>();
students.add(student1);
students.add(student2);
grade.setStudents(students);
String s = JSON.toJSONString(grade,true);
System.out.println(s);
}
@Test
public void test4(){
//
Grade grade = new Grade();
grade.setGradeName("一年");
grade.setGradeId(1);
Student student1 = new Student();
student1.setStudentNo("10001");
student1.setBornDate(new Date());
Student student2 = new Student();
student2.setStudentNo("10002");
student2.setBornDate(new Date());
List studentList = new ArrayList<>();
studentList.add(student1);
studentList.add(student2);
String s = JSON.toJSONString(studentList);
System.out.println(s);
Set students = new HashSet<>();
students.add(student1);
students.add(student2);
grade.setStudents(students);
String s1 = JSON.toJSONString(grade);
System.out.println(s1);
Map map = new HashMap<>();
map.put("key1",student1);
map.put("key2",student2);
String giao = JSON.toJSONString(map,true);
System.out.println(giao);
//
}
@Test
public void test5(){
//
Student student1 = new Student();
student1.setStudentNo("10001");
student1.setBornDate(new Date());
String giao = JSON.toJSONString(student1);
System.out.println(giao);
//
}
}
List[{对象}]
Set{[{对象}]}
Map{对象}