繁体   English   中英

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

[英]Get data with post using axios in node.js

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

更改服务器支持的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);
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM