这一问题的根本原因是跨源资源共享(CORS)问题。由于在通过Axios发送POST请求时请求头中没有包含对跨域请求的授权,从而导致了CORS错误。
解决该问题的方法是,需要在Axios实例中添加相关的跨域请求授权头,如下所示:
import axios from 'axios';
const api = axios.create({
baseURL: 'https://api.vimeo.com',
headers: {
'Authorization': 'Bearer ', // replace with your Vimeo access token
'Content-Type': 'application/json',
'Accept': 'application/vnd.vimeo.*+json;version=3.4', // specify Vimeo API version
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, PUT, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Authorization, Content-Type, Accept, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Origin',
'Access-Control-Expose-Headers': 'Authorization, Content-Type, Accept, DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Origin',
},
});
api.post('/video/', {
// specify POST parameters here
}).then((response) => {
console.log(response.data);
}).catch((error) => {
console.error(error);
});
其中,Access-Control-Allow-Origin
等头部信息的添加可根据API提供商提供的相关文档进行调整。
通过增加相关跨域请求授权头部信息,即可在Axios中正常发送POST请求并返回响应结果。