1.编写前端页面 + Ajax请求
-
表单域 form (三大部分:form、表单域、提交按钮 type="submit"。 每个input需要有名字!否则无法被serialize()添加进数据中)
-
为submit添加on监听事件。1.阻止默认行为 2.通过 form选择器.serialize( )把表单的所有数据添加进data中(务必每个input都有个name属性)
-
通过jquery发送ajax请求!规定好url、type、method、data、success等等
-
特别注意:若后端返回的是 @ResponseBody String 则 type=”text“ ; 若后端返回的是json,则type=”JSON“
Document
2.编写后端响应(只说明数据交换部分。详细知识点请看后端的SpringMVC、Springboot 等笔记)
-
首先,需要解决跨域问题。 在Controller类上添加一个 @CrossOrigin 即可解决跨域问题
-
添加 @RequestMapping() 作为前端ajax 发送url的地址
-
@ResponseBody 让返回的String为text类型 / 让返回的对象自动转为json类型
-
Controller内的方法,需要使用 对象 作为参数,接收来自前端的参数
package com.example.module1.controller;
import com.example.module1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@CrossOrigin
public class TestController {
@RequestMapping("/test")
@ResponseBody
public String test(User user){
System.out.println(user);
return "success";
}
}



