如何从使用post获取数据的api获取数据?这是我的代码,但是当我记录数据时,api不会返回任何内容。
我认为您尝试传递的数据作为请求正文是为了对数据进行网址编码,所以试试这个
axios.post('https://example.com/api?function=getSomething')
.then(function(response) {
console.log(response.data);
});
更改服务器支持的Content-Type
。 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);
})
jsfiddle中的代码,但Access-Control-Allow-Origin
也会收到错误,您可以代理自己或更改服务器原始访问权限。