【问题标题】:REST API, delete request, error with CORS specification or query?REST API、删除请求、CORS 规范或查询错误?
【发布时间】:2020-12-23 20:13:42
【问题描述】:

基本上,我在 Node.js 服务器和 Angular 2 前端的 REST api 应用程序中的 DELETE 操作出现错误

浏览器中的错误信息:

CORS 策略已阻止从源“http://localhost:4200”访问“http://localhost:800/api/admin/deleteArticle?id=5f5244b8376ddd1d0c70ef52”处的 XMLHttpRequest:对预检请求的响应不” t 通过访问控制检查:它没有 HTTP ok 状态。

删除 http://localhost:800/api/admin/deleteArticle?id=5f5244b8376ddd1d0c70ef52 net::ERR_FAILED

ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://localhost:800/api/admin/deleteArticle", ok: false, ...}

前端-发送(Angular):

public deleteArticle(articleId: string) {
    const httpParams = new HttpParams().set('id', articleId);
    const options = { params: httpParams };
    return new Promise((res, rej) => {
      this.http.delete('http://localhost:800/api/admin/deleteArticle', options)
        .subscribe((serverResponse: any) => {
          console.log(serverResponse);
        });
    });
  }

后端服务器(Node.js):

app.js

const express = require('express');
const path = require('path');
const bodyParser = require("body-parser");

const app = express();

const adminRoutes = require('./routes/adminRoutes');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers", "application/x-www-form-urlencoded",
    "Origin, X-Requested-Width, Content-Type, Accept, Authorization"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  )
  next();
});

app.use('/api/admin', adminRoutes);

adminRoutes.js


const router = express.Router();

router.delete('/deleteArticle:id', checkAuth, adminController.deleteArticle);

// everything is OKEY with checkAuth middleware

adminController.js

exports.deleteArticle = (req, res, next) => {
    const articleData = req.query;
    console.log(articleData)
 }

看起来像 CORS 错误,但我在设置 DELETE 操作 (app.js) 中注册,所以一定没有错误:(

你能帮我解决这个问题吗?

提前谢谢你!!

【问题讨论】:

  • 愚蠢的问题:你真的在使用800端口吗?似乎,通常使用 8000。可能是问题(?)。
  • R.理查兹,感谢您的参与,端口号一切正常,Frost 有解决方案并且它正在工作(cors 库),我认为一定有一些小的配置错误,因为它在上周五之前运行良好(我仍然会尝试找到它)

标签: node.js angular rest express cors


【解决方案1】:
const express = require('express');
const path = require('path');
const bodyParser = require("body-parser");

const app = express();

var cors = require('cors');
app.use(cors()); // Use this after the variable declaration

const adminRoutes = require('./routes/adminRoutes');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use((req, res, next) => {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader(
    "Access-Control-Allow-Headers", "application/x-www-form-urlencoded",
    "Origin, X-Requested-Width, Content-Type, Accept, Authorization"
  );
  res.setHeader(
    "Access-Control-Allow-Methods",
    "GET, POST, PATCH, PUT, DELETE, OPTIONS"
  )
  next();
});

app.use('/api/admin', adminRoutes);

【讨论】:

  • 使用这两行var cors = require('cors');app.use(cors());
猜你喜欢
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2017-05-04
  • 2018-03-01
  • 2018-06-07
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多