官方说明:
axios会帮我们 转换请求数据和响应数据 以及 自动转换 JSON 数据
在 axios 源码中发现下面这段内容:(很关键)
Get方式正确传参,node.js后端获取参数
axios 请求方式: 注意: 请求的参数写法是 { params: {orderId: this.orderId }}
axios. get ( "/users/orderDetail" , { params: {
orderId: this . orderId
} } ) . then ( ( res) => {
if ( res. data. status== '0' ) {
this . orderTotal = res. data. result. orderTotal;
}
} )
node.js 后端获取参数方式: 切记: 接收get参数用的是req.param ,不带s
router. get ( "/orderDetail" , function ( req, res, next) {
let userId = req. cookies. userId;
let orderId = req. param ( "orderId" ) ;
console. log ( orderId) ;
} )
Post方式正确传参,node.js后端获取参数
axios 请求方式: 注意: 请求的参数写法是{参数名:参数值} 即 {addressId: this.delAddressId}
axios. post ( "/users/deleteAddress" , { addressId: this . delAddressId} ) . then ( ( res) = > {
if ( res. data. status == 0 ) {
this . getAddressList ( ) ;
this . isMdShow = false ;
}
} )
node.js 后端获取参数方式: 切记: 接收post参数用的是req.body .paramName
router. post ( "/payOrderForm" , function ( req, res, next) {
let userId = req. cookies. userId;
let addressId = req. body. addressId;
let orderTotal = req. body. orderTotal;
} )