一、axios post請求ashx
一般處理程序(ashx)的好處就是容易上手,直接按住寫就行了,但是需要對提供的參數處理一下,不然后端接收不到
var params={ user:this.yhm, pwd:this.$md5(this.pwd).toUpperCase(), };
//Post方法的封裝 axiosPost:function(url,params){ return new Promise((resolve, reject) => { this.$axios({ url: url, method: 'post', data: params, transformRequest: [function(data) { let ret = '' for(let it in data) { ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' } console.log(ret) return ret }], headers: { 'Content-Type':'application/json' } }) .then(res=>{ resolve(res.data); }) }); },
二、axios POST請求webapi
這里的webapi我是使用的 .net core3.1 webapi,有控制器、路由等很是好用,post時參數是json格式的,所以在vue中就不需要再對參數進行轉換了
//Post方法的封裝 axiosPost:function(url,params){ return new Promise((resolve, reject) => { this.$axios({ url: url, method: 'post', data: params, // transformRequest: [function(data) { // let ret = '' // for(let it in data) { // ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' // } // console.log(ret) // return ret // }], headers: { 'Content-Type':'application/json' } }) .then(res=>{ resolve(res.data); }) }); },