【问题标题】:405 Method Not Allowed using DELETE with Restify and CORS405 方法不允许使用带有 Restify 和 CORS 的 DELETE
【发布时间】:2018-03-21 07:20:32
【问题描述】:

这主要是为了在本地开发服务。

我收到了一个405 method not allowed,其中包含使用 Restify 和 CORS 的 DELETE 请求。我想我一定忽略了一些东西。非常感谢一些新鲜的眼睛指出我做错了什么。

我确实知道restify-cors-middleware,但我没有使用它,因为它没有很好地记录,而且我无法正确配置它。

相反,我为 Restify 实现了自己的 CORS 配置。它适用于 GET 和 POST,但不适用于 DELETE。

// allows localhost to work
if (process.env.DEV === 'true') {
  app.pre((req, res, next) => {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With');
    res.header('Access-Control-Allow-Credentials', 'true');
    res.header('Access-Control-Allow-Methods', 'DELETE, GET, POST, OPTIONS, PUT');
    res.header('access-control-max-age', 86400);

    return next();
  });

  app.opts('/.*/', (req, res, next) => {
    res.send(200);
    return next();
  });
}

选项预检:

请求

URL: http://localhost:8000/f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe
Request Method:OPTIONS
Status Code:200 OK
Remote Address:[::1]:8000
Referrer Policy:no-referrer-when-downgrade

响应标头

HTTP/1.1 200 OK
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: DELETE, GET, POST, OPTIONS, PUT
access-control-max-age: 86400
Date: Tue, 10 Oct 2017 01:04:57 GMT
Connection: keep-alive
Transfer-Encoding: chunked

请求标头

OPTIONS /f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: DELETE
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

删除请求:

请求

URL: http://localhost:8000/f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe
Request Method:DELETE
Status Code:405 Method Not Allowed
Remote Address:[::1]:8000
Referrer Policy:no-referrer-when-downgrade

响应标头

HTTP/1.1 405 Method Not Allowed
Server: Planet Timelapse
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Accept, Authorization, Content-Type, Content-Disposition, Origin, X-Requested-With
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: DELETE, GET, POST, OPTIONS, PUT
access-control-max-age: 86400
Allow: OPTIONS
Content-Type: application/json
Content-Length: 61
Date: Tue, 10 Oct 2017 01:04:57 GMT
Connection: keep-alive

请求标头

DELETE /f388798f20e0d496023812a05109ea5276d2eb3d41f8eb5b58c9d43da9b7a001-leWWTe HTTP/1.1
Host: localhost:8000
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
accept: application/json
Origin: http://localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Referer: http://localhost:3000/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

【问题讨论】:

    标签: cors restify


    【解决方案1】:

    您似乎还没有为您的 DELETE 方法注册处理程序。响应显示:

    允许:选项

    也许您没有正确连接处理程序?

    【讨论】:

      猜你喜欢
      • 2021-02-04
      • 2013-03-13
      • 2018-06-16
      • 2014-01-09
      • 2016-08-05
      • 2023-02-17
      • 2019-10-28
      • 2020-09-15
      • 1970-01-01
      相关资源
      最近更新 更多