get方法
URL:http://localhost:8080/findUser?name=tomact&age=18
URL:http://localhost:8080/findUser?name=tomact&age=18&sex=男
传参简化
resful风格URL:http://localhost:8080/findUser/tomact/18/男
1.要求 数据的位置结构一旦确定不能修改
2.参数与参数之间通过斜杠分割
3.resful风格数据适合各种请求类型(get post)
请求类型 get、post、put、delete(常用)
resFul语法:
参数与参数之间用斜杠分割
需要接收的参数用{} 包裹
参数接收时采用@patnvariable 注解取值
如果{name}的属性和对象的属性名称一致 则可以使用对象接收 调用对象的set方法为属性辅助
JSON
JSON 是一种存储和交换数据的语法。
json本质就是字符串, json数据 是用在 语言 里面使用的, ( 语言有java,c#,js )
json对象:的意思就是一组键值对的数据对象,在JavaScript里面就是JavaScript的对象,在c#(控制器就叫c#写法),java里面就是java的json对象(类/VO),不止JavaScript里面有json,c#控制器也会用json
JSON 数据- 名称和值
JSON 数据写为名称/值对。 名称/值由字段名称构成,后跟冒号和值:
所以一般的json格式是:
name={name01:“”,name02={ name02:“”,name021:“”} }
key“”可写可不写
数组格式
value里面可以嵌套
[100,true,["a","b"],{name:"张三",hobby:["厨房",“睡觉”,”打豆豆”]}]
前段访问后端服务器,一般采用Ajax方式进行数据传递,后端服务器返回前段页面 通常采用json格式数据
后端服务器接收前段的参数用servlet机制
@ResponseBody
如果反回String字符串 就把字符串本身返还给前端
{"id":null,"name":"tomact","age":18,"sex":null}
Ajax(重点重点 )
1.发起ajax请求
2.不同的服务器之间发送Ajax请求是会有跨域 问题
3.解决跨域问题要通过注解搞定 @CrossOrigin
// var url="http://localhost:8080/hello"
// axios.get(url)
// .then(function(result){//回调函数
// console.log(result)
// })
//var 关键字没有作用域的概念
// axios为了接收后端服务器数据,利用promise对象封装参数
let url="http://localhost:8080/axios/getUserById?id=10"
axios.get(url)
.then(function(promise){//回调函数
console.log(promise.data)
})
java代码
@Controller//将类交给SpringMVC,springmvc交给spring容器管理
@ResponseBody //将数据转化为”特殊字符串“返回
@CrossOrigin//允许跨域
public class UserController {
/**
* url地址:http://localhost:8080/hello
* http://localhost:8080/dog
* http://localhost:8080/cat get请求
*/
@RequestMapping("/hello")
public String hello() {
return "你好,SpringMVC";
}
get请求
先写业务需求
根据id查询业务信息
var 关键字没有作用域的概念
let相当于var 有作用域 更加安全
const 定义常量的
Axios-get 对象参数写法
将数据存入list集合中
@RequestMapping("/getUserByNA")
public List<User> getUserByNA(User user){
List<User> list= new ArrayList<>();
list.add(user);
list.add(user);
return list;
let use2={name:"tomact",age:"10"}
let url3=`http://localhost:8080/axios/getUserByNS/${use2.name}/${use2.sex}`
axios.get(url3)
.then(function(promise){
console.log(promise.data)
}) -