表达和获取:类型错误:尝试获取资源时出现网络错误

ora*_*ora 2 javascript fetch express

我正在使用express、fetch 和firefox。这是代码

async updateTutorial() {
const requestOptions = {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(this.currentTutorial)
    };

  await fetch("https://localhost:9000/tutorial/update" , requestOptions);


}
Run Code Online (Sandbox Code Playgroud)

在 Firefox 中,我看到此错误: TypeError: NetworkError 当尝试获取资源时我不知道这是什么类型的问题。

Mas*_*ind 5

首先,你必须使用http而不是https。

要修复 cors 错误,您必须mode:"no-cors"在请求选项中指定。

async updateTutorial() {
 
          const requestOptions = {
            method: "POST",
            mode: "no-cors",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(this.tutorial)
        } 

        await fetch("http://localhost:9000/tutorial/update" , requestOptions);
   }
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助。如果您遇到任何问题,请告诉我。

  • 模式:“no-cors”就像一个魅力。谢谢你! (4认同)