跨域请求被阻止:同源策略禁止在https:// 2020 node js读取远程资源

时间:2020-10-20 14:33:10

标签: node.js angular express server cloud9-ide

基于此问题提出了很多问题,但没有一个解决了我的问题。 我正在使用cloud9 IDE进行开发。我正在尝试使用API​​从节点服务器到角度项目接收数据。我的node.js服务器具有所需的所有CORS标头。但是我继续收到错误。 这是我的服务器代码:

const got = require('got');
const yourArray = [...];

async function run() {
    for (let [index, item] of yourArray.entries()) {
        try {
            let result = await got(item.url);
            // do something with the result
        } catch(e) {
            // either handle the error here or throw to stop further processing
        }
    }
}

run().then(() => {
    console.log("all done");
}).catch(err => {
    console.log(err);
});

这是firefox浏览器上的错误:

enter image description here

是IDE引起问题吗?还是我错过了什么?请帮忙!

1 个答案:

答案 0 :(得分:0)

我认为这是因为您仅在后端启用了获取动词

更改此:

res.header('Access-Control-Allow-Methods', 'GET');

为此:

res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');