最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

origin 'http:127.0.0.1:5174' 已被 CORS 策略阻止:

网站源码admin42浏览0评论

origin 'http://127.0.0.1:5174' 已被 CORS 策略阻止:

origin 'http://127.0.0.1:5174' 已被 CORS 策略阻止:

APP.js in backened

import express from "express";
import userRouter from "./routes/user.js";
import taskRouter from "./routes/task.js";
import { config } from "dotenv";
import cookieParser from "cookie-parser";
import { errorMiddleware } from "./middlewares/error.js";
import cors from "cors";

export const app = express();

config({
  path: "./data/config.env",
});

// Using Middlewares
app.use(express.json());
app.use(cookieParser());

app.use(
  cors({
    // origin: ["http://127.0.0.1:5174/api/v1"],
    origin: ["http://localhost:3000/api/v1"],
    methods: ["GET", "POST", "PUT", "DELETE"],
    credentials: true,
  })
);

// Using routes
app.use("/api/v1/users", userRouter);
app.use("/api/v1/task", taskRouter);

app.get("/", (req, res) => {
  res.send("Nice working");
});

// Using Error Middleware
app.use(errorMiddleware);


main.jsx from frontened

import React, { useState } from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import "./styles/app.scss";
import { createContext } from "react";

export const server = "";
// export const server = "https://localhost:3000/api/v1";

export const Context = createContext({ isAuthenticated: false });

const AppWrapper = () => {
  const [isAuthenticated, setIsAuthenticated] = useState(false);
  const [loading, setLoading] = useState(false);
  const [user, setUser] = useState({});



  return (
    <Context.Provider
      value={{
        isAuthenticated,
        setIsAuthenticated,
        loading,
        setLoading,
        user,
        setUser,
      }}
    >
      <App />
    </Context.Provider>
  );
};

ReactDOM.createRoot(document.getElementById("root")).render(
  <React.StrictMode>
    <AppWrapper />
  </React.StrictMode>
);

从来源“http://127.0.0.1:5174”访问“”的 XMLHttpRequest 已被 CORS 策略阻止:响应预检请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。

为什么我在登录和登录我的网页时出现此错误,甚至我已经使用了所有 cors perfect ??

我已经在这个链接“”上部署了我的 api,它工作正常但 cors 策略错误没有解决

回答如下:
发布评论

评论列表(0)

  1. 暂无评论