get请求方式
GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改、增加数据,不会影响资源的内容,即该请求不会产生副作用。无论进行多少次操作,结果都是一样的。
服务器
// 传统的URLapp.get('/axios', (req, res) => {res.send('axios get 传递参数' + req.query.id)})// Restful 的URLapp.get('/axios/:id', (req, res) => {res.send('axios get(Restful) 传递参数' + req.params.id)})app.get('/data', (req, res) => {res.send('Hello World !')})
客户端
axios.get('http://localhost:3000/adata').then(function (ret) {console.log(ret.data);});// 传统URL 只需要axios.get('http://localhost:3000/axios?id=123').then(function (res) {console.log(res.data);});axios.get('http://localhost:3000/axios/123').then(function (res) {console.log(res.data);});// 传统URLaxios.get('http://localhost:3000/axios', {params: {id: 234}}).then(function (ret) {console.log(ret.data);});
delete请求方式
DELETE请求顾名思义,就是用来删除某一个资源的,该请求就像数据库的delete操作。
服务器
// 传统的URLapp.delete('/axios', (req, res) => {res.send('axios delete 传递参数' + req.query.id)})// Restful 的URLapp.delete('/axios/:id', (req, res) => {res.send('axios delete(Restful) 传递参数' + req.params.id)})
客户端
axios.delete('http://localhost:3000/axios?id=123').then(function (res) {console.log(res.data);});axios.delete('http://localhost:3000/axios/123').then(function (res) {console.log(res.data);});axios.delete('http://localhost:3000/axios', {params: {id: 234}}).then(function (res) {console.log(res.data);});
post请求方式
POST请求同PUT请求类似,都是向服务器端发送数据的,但是该请求会改变数据的种类等资源,就像数据库的insert操作一样,会创建新的内容。几乎目前所有的提交操作都是用POST请求的。
服务器
// 传统的URLapp.post('/axios', (req, res) => {res.send('axios post 传递参数' + req.body.id + '---' + req.body.uname + '---' +req.body.pwd)})// Restful 的URLapp.post('/axios/:id', (req, res) => {res.send('axios post(Restful) 传递参数' + req.params.id + '---' + req.body.uname + '---' +req.body.pwd)})
客户端
axios.post('http://localhost:3000/axios/111', {uname: 'kun',pwd: 123,}).then(function (res) {console.log(res.data);})// ----------------------------------------------------------------var params = new URLSearchParams();params.append('uname', 'ikun');params.append('pwd', '111');params.append('id', 99999);axios.post('http://localhost:3000/axios', params).then(function (res) {console.log(res.data);})
put请求方式
与GET不同的是,PUT请求是向服务器端发送数据的,从而改变信息,该请求就像数据库的update操作一样,用来修改数据的内容,但是不会增加数据的种类等,也就是说无论进行多少次PUT操作,其结果并没有不同。
服务器
// 传统的URLapp.put('/axios', (req, res) => {res.send('axios put 传递参数' + req.body.id + '---' + req.body.uname + '---' +req.body.pwd)})// Restful 的URLapp.put('/axios/:id', (req, res) => {res.send('axios put(Restful) 传递参数' + req.params.id + '---' + req.body.uname + '---' +req.body.pwd)})
客户端
axios.put('http://localhost:3000/axios/123', {// 传参uname: 'lisi',pwd: 123}).then(ret => {console.log(ret.data);})/* URLSearchParams 接口定义了一些实用的方法来处理 URL 的查询字符串。我们调用new URLSearchParams()会返回一个 URLSearchParams 对象实例 */var params = new URLSearchParams();params.append('uname', 'kun');params.append('pwd', '222');params.append('id', 222);axios.put('http://localhost:3000/axios', params).then(function (res) {console.log(res.data);})
POST、DELETE、PUT、GET 分别对应着
增 ———- 删 ——— 改 —— 查
自我激励
在人生的道路上,让我们时刻自我激励着吧!永远!永远!