一般这种问题是因为axios的请求头没有设置好,导致发送的数据无法被服务器解析。我们需要设置一下请求头来规避这个问题。
下面是一个Axios post请求发送Json数据的示例代码:
import axios from 'axios';
axios.post(url, {
name: 'John',
age: 30
}, {
headers: {
'Content-Type': 'application/json'
}
}).then(response => {
console.log(response);
}).catch(error => {
console.error(error);
});
在上面的代码中,我们指定了请求的数据是Json格式,然后在请求头中设置了Content-Type。这样就可以成功地发送数据并获得服务器的响应了。