在 node.js 中使用 axios 通过 post 获取数据

Get data with post using axios in node.js

如何从使用 post 获取数据的 api 获取数据?这是我的代码,但是当我记录数据时 api 没有 return 任何东西。

更改Content-Type服务器支持的。 Qsquerystring 库。

axios.post('https://example.com/api', Qs.stringify({
    'function': 'getEvents'
}), {
    withCredentials: true,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
}).then(function(response) {
    console.log(response.data);
}, function(error) {
    console.log(error);
})

jsfiddle中的代码,但Access-Control-Allow-Origin也出现错误,您可以代理自己或更改服务器源访问允许。

我认为您尝试作为请求正文传递的数据应该是 url 编码的表单数据,所以,试试这个

axios.post('https://example.com/api?function=getSomething')
.then(function(response) {
      console.log(response.data);
    });