我在从https://www.myapifilms.com应用程序接口获取数据时遇到了CORS错误。我不想使用像cors-anywhere或浏览器扩展这样的第三方网站。如何才能在不出现cors错误的情况下获取数据?我尝试添加了'no-cors','cors‘模式,我也尝试添加了头部'Access-Control-Allow-Origin':'*’。这也不管用。
下面是我用来获取数据的代码:
const handleData = function(url, callback, method = 'GET', body = null) {
fetch(url, {
method: method,
body: body,
headers: { 'content-type': 'application/json' }
})
.then(function(response) {
if (!response.ok) {
throw Error(`Probleem bij de fetch(). Status Code: ${response.status}`);
} else {
console.info('Er is een response teruggekomen van de server');
return response.json();
}
})
.then(function(jsonObject) {
console.info('json object is aangemaakt');
console.info('verwerken data');
callback(jsonObject);
})
.catch(function(error) {
console.error(`fout bij verwerken json ${error}`);
});发布于 2019-11-20 21:03:28
应从API端而不是从UI启用CORS
您遇到了CORS问题,因为API不允许跨域
所以应该在API端配置'Access-Control-Allow-Origin': '*'。
添加上述配置的方法因语言而异(Node、Java、..etc)
https://stackoverflow.com/questions/58954878
复制相似问题