javascript - Cors and headers -


i'm having trouble cors , headers. have below middleware:

res.header('access-control-allow-origin', '*'); res.header('access-control-allow-methods', 'get,put,post,delete'); res.header('access-control-allow-headers', 'content-type,x-access-token,authorization'); next(); 

after have middleware check token:

const token = req.body.token || req.query.token || req.headers['x-access-token'];      if (token) {         jwt.verify(token, config.jwtkey, (err, decoded) => {             if(err) {                 return res.json({success: false, errmsg: 'wrong key'});             } else {                 req.decoded = decoded;                 next();             }         });     } else {         return res.status(403)             .send({                 success: false,                 message: "no token provided"             });     } 

but when log req.headers:

{ host: 'localhost:4556',   connection: 'keep-alive',   'access-control-request-method': 'post',   origin: 'http://localhost:4200',   'user-agent': 'mozilla/5.0 (macintosh; intel mac os x 10_11_6) applewebkit/537.36 (khtml, gecko) chrome/59.0.3071.115 safari/537.36',   'access-control-request-headers': 'authorization,content-type,x-access-token',   accept: '*/*',   dnt: '1',   referer: 'http://localhost:4200/posts',   'accept-encoding': 'gzip, deflate, br',   'accept-language': 'sv,en-us;q=0.8,en;q=0.6' } 

there no "x-access-token" in headers, except in "access-control-request-headers". , it's name. must wrong, found when googled use access-control-allow-headers.

you looking @ preflight options request. can triggered number of conditions, 1 of "sets non-standard header" (like x-access-token).

the browser won't make post request (with x-access-token header) until server responds options request giving permission.

you'll need exclude options requests token checking middleware don't send 403 in response preflight (which never include token).


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -