Axios POST 请求将所有参数作为一个主体键传递



我有一个简单的Axios POST请求:

const data = JSON.stringify({
to: receiver,
from: sender,
body: message
});
axios.post(window.location.origin + '/sms/outgoing', data)

我的问题是我的 api 按如下方式读取请求正文:

{ '{"to":"12345","from":"54321","body":"message"}': '' }

当我希望它是这样的:

{"to":"12345","from":"54321","body":"message"}

我哪里出错了?

不需要JSON.stringify方法。

const data = {
to: receiver,
from: sender,
body: message
};
axios.post(window.location.origin + '/sms/outgoing', data)

但是您需要 JSON.stringify 方法来遵循此后端 API 调用

app.route(window.location.origin + '/sms/outgoing',(req,res)=>{
let data = JSON.parse(req.body)
console.log(data) //get {"to":"12345","from":"54321","body":"message"}
})

如果要从 JSON 字符串解析 JSON 对象,只需使用JSON.parse函数

const object = JSON.parse({
someJSONString: true,
})

相关内容

最新更新