简体   繁体   English

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

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

How can I get data from an api that uses post to get data? 如何从使用post来获取数据的api中获取数据? Here is my code but the api does not return anything when I log the data. 这是我的代码,但是当我记录数据时api不返回任何内容。

Change Content-Type which the server support. 更改服务器支持的Content-Type Qs is querystring library. 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);
})

Code in jsfiddle , but it also get an error by Access-Control-Allow-Origin , you may proxy yourself or change the server origin access allow. jsfiddle中的代码,但是Access-Control-Allow-Origin也会出错,您可以代理自己或更改服务器原始访问权限。

i think the data you are trying to pass as the requests body is meant to be url encoded form data so, try this 我认为您尝试作为请求正文传递的数据是要通过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