来自网络浏览器的 POST 请求会抛出错误,而邮递员不会。如何防止 Axios 在值周围加上引号?



我正在使用Formik和React对.NetCore实体框架API进行POST。

在Postman中,它张贴得很好,没有任何错误。然而,当我从web浏览器进行POST时,我会收到一个错误。

在检查了Postman请求和浏览器发送的请求后,我看到的唯一区别是:

邮差作品-价值无报价:

"galaxyTypeId": 1,

Axios Post-它围绕价值报价:

"galaxyTypeId": "4",

我在浏览器中得到这个错误:

POST https://localhost:44376/api/getformdata 400
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:61)

以下是代码的相关部分:

<Formik
initialValues={{
gameAdminEmail: "",
adminName: "",
phone: "",
galaxyTypeId: 1,
starId: 1,
description: "",
gameUrl: "",
}}
onSubmit={async values => {
await new Promise(resolve => setTimeout(resolve, 500));
axios({
method: "POST",
url: "https://localhost:44376/api/getformdata",
data: values
});
console.log(JSON.stringify(values, null, 2));
}}
>

有没有办法防止Axios在数值周围加引号?

谢谢!

我在这里查看这个代码笔,问题没有重现。https://codepen.io/OpenGG/pen/xaGKEP

var payload = { "galaxyTypeId": 45 }
axios.post('//httpbin.org/post', payload)
.then(res => {
const pre = document.createElement('pre');
pre.textContent = JSON.stringify(res.data, null, '  ');
document.body.appendChild(pre);
})
.catch(err => {
document.body.textContent = err.stack;
});

你有回购的链接吗?

最新更新