代码之家  ›  专栏  ›  技术社区  ›  Flem

不使用cookie发送删除、放置。。。但是POST可以

  •  0
  • Flem  · 技术社区  · 1 年前

    大家好,我有一个小项目,但我需要帮助:) 我没有说为什么我的cookie不发送,当我使用方法删除,放。。。 但当我用邮件发送请求时,或者得到是可以的。。。

    前端

    export const deleteAccount = async (idAccount) => {
      try {
        const response = await axios.delete(
          `${process.env.REACT_APP_ENV}/subaccount/${idAccount}`,
          {
            withCredentials: true,
          }
        )
        return response.data
      } catch (error) {
        console.error("Error deleting account:", error)
        throw error
      }
    }
    

    后端

    var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');
    require('dotenv').config();
    
    var authRouter = require('./routes/private/auth');
    var publicOfferRouter = require('./routes/public/offer');
    var privateOfferRouter = require('./routes/private/offer');
    var privateSubAccountRouter = require('./routes/private/subAccount');
    var privateUserRouter = require('./routes/private/user');
    var privateCtaRouter = require('./routes/private/cta');
    var app = express();
    
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'jade');
    
    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(express.static(path.join(__dirname, 'public')));
    
    // Add headers before the routes are defined
    app.use(function (req, res, next) {
      // Website you wish to allow to connect
      res.setHeader('Access-Control-Allow-Origin', process.env.DOMAIN);
    
      // Request methods you wish to allow
      res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    
      // Request headers you wish to allow
      res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    
      // Set to true if you need the website to include cookies in the requests sent
      // to the API (e.g. in case you use sessions)
      res.setHeader('Access-Control-Allow-Credentials', 'true');
    
      // Pass to next layer of middleware
      next();
    });
    

    以及消息错误:

    仪表板:1在“”访问XMLHttpRequesthttp://localhost:3001/subaccount/56'来自原点'http://localhost:3000'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:它的HTTP不正常

    0 回复  |  直到 1 年前
    推荐文章