【问题标题】:Can I use HTTP COPY Request with axios?我可以在 axios 中使用 HTTP COPY 请求吗?
【发布时间】:2020-12-27 20:26:49
【问题描述】:

要与 CouchDB 实例交互,我需要发送一个 HTTP“COPY”请求。 (非标准 http 请求)。

如果我使用 Postman 发送请求,它可以正常工作。但是在我的 web 应用程序中,我使用 axios,并且使用 axios 发送 COPY 请求会导致错误。 (奇怪的是,CORS 错误)。

我是这样使用axios的:

axios(
   { method:'copy',
    url: '....',
    ...
   }
)

我找不到太多关于 HTTP“COPY”请求的信息,我想知道 axios 甚至浏览器是否无法发送 COPY 请求?

任何小费都会很棒!谢谢。

控制台:

Access to XMLHttpRequest at 'http://localhost:5984/somedoc/505282119b93327fa4fecbef68026db4_cp' from origin 'http://localhost:8080' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

好的,现在我知道有一个预检请求并且浏览器说预检以某种方式失败。

我看到“方法不允许”,但 COPY 在“允许”下列出。

我不明白我在 axios 中的请求有什么问题,而它在 Postman 中运行良好。

(顺便说一句。所有其他请求:get、put、delete.. 在 axios 中工作)

这就是我在 Chrome 中看到的:

General:
Request URL: http://localhost:5984/somedoc/505282119b93327fa4fecbef68026db4_cp
Request Method: OPTIONS
Status Code: 405 Method Not Allowed
Remote Address: 127.0.0.1:5984
Referrer Policy: no-referrer-when-downgrade

Response Headers:
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:8080
Access-Control-Expose-Headers: content-type, cache-control, accept-ranges, etag, server, x-couch-request-id, x-couch-update-newrev, x-couchdb-body-time
Allow: DELETE,GET,HEAD,POST,PUT,COPY
Cache-Control: must-revalidate
Content-Length: 85
Content-Type: application/json
Date: Wed, 09 Sep 2020 12:36:30 GMT
Server: CouchDB/2.3.1 (Erlang OTP/19)
X-Couch-Request-ID: 61447fcae4
X-CouchDB-Body-Time: 0

Request Headers:
Accept: */*
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,la;q=0.6
Access-Control-Request-Headers: authorization,content-type,destination
Access-Control-Request-Method: COPY
Cache-Control: no-cache
Connection: keep-alive
Host: localhost:5984
Origin: http://localhost:8080
Pragma: no-cache
Referer: http://localhost:8080/
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36

【问题讨论】:

  • 你说你得到错误 - 什么错误?请edit您的问题显示您运行的一些实际代码,以及它产生的确切错误。
  • Strangely CORS errors - 如果是跨域请求,这并不奇怪......
  • 浏览器可以发送任何方法作为方法 - 如果方法不被允许,那么服务器将响应 405 错误

标签: javascript axios copy httprequest


【解决方案1】:

我想我现在明白了:

couchDB 作为 HTTP 服务器存在问题。 Couch 不知道 OPTION 请求,因此 CORS 预检失败。

相同的 COPY 请求在 Postman 中有效,因为 Postman 不关心 CORS 并且不执行 prefligt 请求。

解决方案好像在couchdb前面有个代理...

【讨论】:

    猜你喜欢
    • 2010-11-29
    • 2017-10-05
    • 2020-08-26
    • 2019-12-07
    • 1970-01-01
    • 2018-01-04
    • 2023-03-12
    • 2023-01-10
    • 2019-11-13
    相关资源
    最近更新 更多