te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Axios not sending custom headers in request (possible CORS issue) - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Axios not sending custom headers in request (possible CORS issue) - Stack Overflow

programmeradmin1浏览0评论

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

I'm using it like this:

axios({
  method: 'get',
  url: 'www.my-url',
  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

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.

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

I'm using it like this:

axios({
  method: 'get',
  url: 'www.my-url.',
  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.

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.

Share Improve this question asked May 30, 2018 at 11:38 NinethousandNinethousand 5352 gold badges7 silver badges16 bronze badges 8
  • Do you get a CORS related error message printed on the console? – Quentin Commented May 30, 2018 at 11:40
  • Does the browser make a GET request or is it a preflight OPTIONS request? – Quentin Commented May 30, 2018 at 11:40
  • The Access-Control-Request-Headers request header is used when issuing a preflight request to let the server know which HTTP headers will be used when the actual request is made. Why are you sending your custom header as access control ? – Ronn Wilder Commented May 30, 2018 at 12:11
  • can you please show what exactly is your call, all the request headers you are sending. – Ronn Wilder Commented May 30, 2018 at 12:11
  • @RonnWilder — Eh? The OP is trying to send a Custom-Header header. Why wouldn't the browser make a preflight request with a Access-Control-Request-Headers: custom-header header ahead of making the request the OP is trying to make? – Quentin Commented May 30, 2018 at 12:12
 |  Show 3 more ments

2 Answers 2

Reset to default 13 +50

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

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.

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

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

Your axios request is correct. You need to allow your custom headers on server side. If you have your api in php then this code will work for you.

header("Access-Control-Allow-Origin: *");   
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, HEAD");
header("Access-Control-Allow-Headers: Content-Type, Custom-Header");

Once you will allow your custom headers on server side, your axios requests will start working fine.

发布评论

评论列表(0)

  1. 暂无评论