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

Javascript 使用node.js中的axios通过post获取数据,javascript,node.js,axios,Javascript,Node.js,Axios,如何从使用post获取数据的api获取数据?这是我的代码,但当我记录数据时,api不会返回任何内容。更改服务器支持的内容类型Qs是querystring库 axios.post('https://example.com/api', Qs.stringify({ 'function': 'getEvents' }), { withCredentials: true, headers: { 'Content-Type': 'application/x-www-

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

更改服务器支持的
内容类型
Qs
querystring

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);
})

输入代码,但通过访问控制允许源站也会出现错误,您可以代理自己或更改服务器源站访问允许。

更改服务器支持的内容类型
Qs
querystring

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);
})

输入代码,但它也会通过访问控制允许源代码得到一个错误,您可以代理自己或更改服务器源代码访问允许。

我认为您试图传递的数据作为请求主体是url编码的表单数据,因此,请尝试此方法

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

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

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

服务器支持哪种内容类型?多部分/表单数据集标题
multipart/form data
在这个请求中,
axios
默认
内容类型
application/json
。我怎么做?实际上内容类型是
text/html;charset=UTF-8
服务器支持哪种内容类型?多部分/表单数据集标题
multipart/form data
在这个请求中,
axios
默认
内容类型
application/json
。我怎么做?实际上内容类型是
text/html;字符集=UTF-8