Axios未在请求中发送自定义标头(可能是CORS问题) [英] Axios not sending custom headers in request (possible CORS issue)

查看:376
本文介绍了Axios未在请求中发送自定义标头(可能是CORS问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了axios似乎无法随我的请求发送自定义标头的问题.

I'm encountering a problem where axios doesn't seem to send custom headers with my requests.

我正在这样使用它:

axios({
  method: 'get',
  url: 'www.my-url.com',
  headers: { 'Custom-Header': 'my-custom-value' }
})

但是,查看发送到服务器的实际请求,自定义标头似乎不在任何地方.

However, looking at the actual request that is sent to the server, the custom header doesn't seem to be anywhere.

REQUEST HEADERS:
  Accept: */*
  Accept-Encoding: gzip, deflate, br
  Accept-Language: es-ES,es;q=0.9
  Access-Control-Request-Headers: custom-header
  Access-Control-Request-Method: GET
  Connection: keep-alive
  Host: my-url.com

我怀疑这可能是CORS问题(响应标头不包含Access-Control-Allow-Headers中的Custom-Header),但在与API所有者联系之前,我想确定一下.

I suspect it might be a CORS problem (the response headers don't include Custom-Header in Access-Control-Allow-Headers), but I would like to make sure before contacting the API owners about the matter.

推荐答案

好.所以你的情况是预检要求. 当客户端尝试发送自定义标头时,服务器需要验证其是否接受该标头.

Ok. so your case is preflight request. when client tries to send a custom header, server needs to verify that it accepts that header.

因此,在这种情况下,预检选项请求与标头Access-Control-Request-Headers一起发送. 如果服务器响应,它将接受自定义标头.那么实际的请求将被发送.

so in that case, a preflight options request is sent with header Access-Control-Request-Headers. if server responds that it will accept the custom header. then actual request will be sent.

在您的情况下,服务器响应标头-access-control-allow-headers不包含您的自定义标头名称. 那就是为什么它失败了.

in your case server response header - access-control-allow-headers does not contains your custom header name. thats why it failed.

注意:实际的POST请求不包括Access-Control-Request- *标头;仅对于OPTIONS请求才需要它们.请阅读本文以获取更多说明- cors -选项调用

Note: the actual POST request does not include the Access-Control-Request-* headers; they are needed only for the OPTIONS request.Read this article for more explanation - cors - options call

这篇关于Axios未在请求中发送自定义标头(可能是CORS问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆