1、axios中的get请求:
(1)请求方式:
axios .get( 'http://localhost:3000/data/sub?' + 'input=' + this.input + '&password=' + this.password ) .then((res) => { console.log(res.data) }) .catch((err) => { console.log(err.data) })
通过这种直接把传输的内容拼接在连接之后其实是非常不规范的,但是效果是可以实现的,因为第一次接触前后端开发,看到后端写的接口连接地址格式如上,因此第一反应是通过字符串的拼接拼出接口的链接格式。
2、这种写法应该是比较规范的,看起来也好看了一些
axios .get( 'http://localhost:3000/data/sub?',{ params:{ input:this.input, params:this.password } } ) .then((res) => { console.log(res.data) }) .catch((err) => { console.log(err.data) })
通过上面两个方法提交的请求,在Express搭建后端获取到的实际访问链接都是
GET /data/sub?input=123&password=123
对于get方法,Express搭建的后端如果想访问前端传递的数据,通过:
req.query
post发送请求:
axios .post( 'http://localhost:3000/data/sub?',{ input:this.input, params:this.password } ) .then((res) => { console.log(res.data) }) .catch((err) => { console.log(err.data) })
Express获取到的链接:
POST /data/sub?
通过req.body可以获取到传递的数据
req.body