axios数据中的等号导致axios不发送正确的数据对象



我正在尝试将以下json对象发送到REST API。

let json = {
name: name,
type: type,
format: format,
param: param,
};

带有" parameter ","type","format "name"都是琴弦。我用"="如果用户输入空字符串,则作为值。(我公司决定,这不是重点)

我为请求构造这样的数据:

let data = JSON.stringify(json);

JSON对象看起来如下(在控制台中打印出来时):

{"name":"Test","type":"=","format":"=","param":"="}

问题是firefox控制台和接收站点(REST API)向我报告,我将{"name":"Test","type":" "","format":""作为数据发送,这显然不是我想要发送的。

因为我给axios正确的对象,如果我的字符串不等于符号,它可以正确工作,我想这可能是问题所在。有办法解决这个问题吗?也许有一些编码?还是axios的参数?

提前感谢您的帮助

我叫axios方法以下列方式

let request = await this.requestAxios(`${this.$root.restURL}interfaces/${this.interfaceDetailId}?languages=${this.$root.language}`,"PATCH",data);

我的axios方法如下所示:

async requestAxios(url, method, data = "", skipPostLog = false) {
//Prevent a unwanted recursion. Only log when the caller is not log itself
if (!skipPostLog) {
await this.log(
`${method}-Request to endpoint ${url} with data: ${data}`,
`requestAxios()`,
"TRACE"
);
} else if (
url !== `${this.$root.restURL}log?language=${this.$root.language}` &&
skipPostLog
) {
await this.log(`${method}-Request to endpoint ${url}:`, `requestAxios()`, "TRACE");
}
let request;
try {
request = await axios.request({
url,
method,
data,
headers: {
authorization: this.$root.getToken(),
},
});
} catch (e) {
request = e.response;
}
if (request.status === 401) {
eventBus.$emit("showErrorMessage", request.data.message);
}
return request;
},
curl 'http://localhost:3013/v1/interfaces/1?languages=de' -X PATCH -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0' -H 'Accept: application/json, text/plain, */*' -H 'Accept-Language: de,en-US;q=0.7,en;q=0.3' --compressed -H 'Content-Type: application/x-www-form-urlencoded' -H 'authorization: 37ea8c25a695da1ff6afe7b3dbf1ad87' -H 'Origin: http://localhost:8080' -H 'Connection: keep-alive' -H 'Referer: http://localhost:8080/' -H 'Sec-Fetch-Dest: empty' -H 'Sec-Fetch-Mode: cors' -H 'Sec-Fetch-Site: cross-site' -H 'Pragma: no-cache' -H 'Cache-Control: no-cache' --data-raw '{"name":"Test","type":"=","format":"=","param":"="}'

由于您正在自己序列化数据并将字符串传递给axios,因此每个人(可能包括您的服务器)显然都假设数据是表单编码的字符串。你需要添加一个header:

Content-Type: application/json

你传递一个对象,而不是字符串给axios,所以它会为你做所有这些。

相关内容

  • 没有找到相关文章