簡體   English   中英

Socket.io + Node.js CORS 錯誤阻止了我的請求

[英]Socket.io + Node.js CORS ERROR Blocked my req

我正在使用 node 和 socket.io 編寫一個應用程序,它給出了啟用跨源請求的錯誤。

const io = require("./socket").init(server, {
      cors: {
        origin: "*",
        methods: ["GET", "POST"],
      },
    });

這是我的 soket.io 代碼...

    let io;
    module.exports = {
      init: (httpServer) => {
        io = require("socket.io")(httpServer);
        return io;
      },
      getIO: () => {
        if (!io) {
          throw new Error("Socket.io not inittialized");
        }
    return io;
  },
};

提前幫我解決這個問題......

在必須發送的套接字 js 中嘗試此代碼

cors: {
       origin: "*",
       methods: ["GET", "POST"],
      }, 

套接字中的Cors也避免了這個錯誤

   let io;
    module.exports = {
      init: (httpServer) => {
        io = require("socket.io")(httpServer, {
          cors: {
            origin: "*",
            methods: ["GET", "POST"],
          },
        });
        return io;
      },
      getIO: () => {
        if (!io) {
          throw new Error("Socket.io not inittialized");
        }
        return io;
      },
    };

希望對你有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM