不允许将 DELETE 与 Restify 和 CORS 一起使用的 405 方法
405 Method Not Allowed using DELETE with Restify and CORS
这主要是为了在本地开发服务。
我收到一个 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
回应headers
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
请求headers
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
回应headers
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
请求Headers
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
您似乎还没有为 DELETE 方法注册处理程序。响应显示如下:
允许:选项
也许您没有正确连接处理器?
这主要是为了在本地开发服务。
我收到一个 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
回应headers
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
请求headers
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
回应headers
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
请求Headers
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
您似乎还没有为 DELETE 方法注册处理程序。响应显示如下:
允许:选项
也许您没有正确连接处理器?