vue中axios.post的复杂参数传参不支持的解决办法
我的参数格式是这样的,经过测试传到api中不能被正确接收:
let data = [{ Field: "CreateTime", Operator: ">=", Value: _this.beginTime }, { Field: "CreateTime", Operator: "<=", Value: _this.endTime }, { Field: "PatientId", Operator: "=", Value: _this.dynamicTags.join(',') }, { Field: "TaskName", Operator: "=", Value: value }, { Field: "LikeType", Operator: "=", Value: _this.radioLikeType }];
通过在axios中转换一下,即可成功请求:
axios({ method: "post", url: httpUrl + "/api/Original/PushMessage", data: { "": data}, headers: { 'Content-Type': 'application/x-www-form-urlencoded', //指定消息格式 }, transformRequest: [function (e) { // 数据转换的核心代码 function setDate(e) { var t, n, i, r, o, s, a, c = ""; for (t in e) if (n = e[t], n instanceof Array) for (a = 0; a < n.length; ++a) o = n[a], i = t + "[" + a + "]", s = {}, s[i] = o, c += setDate(s) + "&"; else if (n instanceof Object) for (r in n) o = n[r], i = t + "[" + r + "]", s = {}, s[i] = o, c += setDate(s) + "&"; else void 0 !== n && null !== n && (c += encodeURIComponent(t) + "=" + encodeURIComponent(n) + "&"); return c.length ? c.substr(0, c.length - 1) : c } // 数据转换的核心代码结束 return setDate(e) }] }).then(res => { if (res.data.message === 'OK') { this.$message({ type: 'success', message: res.data.data.message }); } else { this.$message({ type: 'error', message: res.data.data.message }); } });