使用node.js中的axios获取数据

问题描述 投票:0回答:2

如何从使用post获取数据的api获取数据?这是我的代码,但是当我记录数据时,api不会返回任何内容。

javascript node.js axios
2个回答
1
投票

我认为您尝试传递的数据作为请求正文是为了对数据进行网址编码,所以试试这个

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

1
投票

更改服务器支持的Content-TypeQsquerystring图书馆。

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也会收到错误,您可以代理自己或更改服务器原始访问权限。

© www.soinside.com 2019 - 2024. All rights reserved.