一、前后端联调之Form Data与Request Payload
post方法之Form Data ‘Content-Type‘: ‘application/x-www-form-urlencoded; charset=UTF-8‘,
post(url, params, timeout) { let token = Cookie.getJSON("auth") ? Cookie.getJSON("auth") : ""; let time = timeout ? timeout : 60000; return axios({ method: ‘post‘, url: url, data: qs.stringify(params), timeout: time, contentType: false, processData: false, headers: { ‘X-Requested-With‘: ‘XMLHttpRequest‘, ‘Content-Type‘: ‘application/x-www-form-urlencoded; charset=UTF-8‘, ‘JSESSIONID‘: token.accessToken } }).then( (response) => { return checkStatus(response) } ).catch( (res) => { return checkCode(res) } ) },
api.post("/enterpriseApi/toolBuild/getPageList", { unitId: this.facilitiesInData.unitId, buildId: this.buildId, buildName: this.buildName, status: this.status, pageNo: this.widgetInfo.pageNo, pageSize: this.widgetInfo.pageSize }).then(res=>{})
postJson方法之Request Payload ‘Content-Type‘: ‘application/json; charset=UTF-8‘,
注意这里data: params里的params不能加qs.stringify(params),而直接是params,因为已经是json了,不需要再一次的序列号,否则就是最下面的情况。
postJson(url, params, timeout) { let token = Cookie.getJSON("auth") ? Cookie.getJSON("auth") : ""; let time = timeout ? timeout : 60000; return axios({ method: ‘post‘, url: url, data: params, timeout: time, contentType: false, processData: false, headers: { ‘X-Requested-With‘: ‘XMLHttpRequest‘, ‘Content-Type‘: ‘application/json; charset=UTF-8‘, ‘JSESSIONID‘: token.accessToken } }).then( (response) => { return checkStatus(response) } ).catch( (res) => { return checkCode(res) } ) },
that.addDataObj.trialId = that.facilitiesInData.id; that.addDataObj.userId = api.getGlobalVal(‘userObj‘).id; that.addDataObj.operatorName = localStorage.getItem("userName"); that.addtableData.forEach((item)=>{ item.unitId = this.facilitiesInData.unitId; }) that.addDataObj.builds = that.addtableData; api.postJson("/enterpriseApi/toolBuild/addBatch", that.addDataObj) .then(res=>{})
加qs.stringify(params):
原文:https://www.cnblogs.com/xiaoxiao529/p/13691096.html