node.js - Ajax GET request: x-access-token header not recieved in backend -


i'm trying send ajax request node.js microservice. request should have in header {'x-access-token': somtoken} , request follows:

$.ajax({      type: "get",    url: "http://localhost:3000/api/books",    headers: {'x-access-token' : sometoken},    success: function(datastring) {         console.log(json.stringify(datastring));    }, error: function(error) {        console.log(json.stringify(error));    } }); 

however, header recieved in microservice following:

{ host: 'localhost:3000', connection: 'keep-alive', 'access-control-request-method': 'get', origin: 'null', 'user-agent': 'mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) chrome/58.0.3029.96 safari/537.36', 'access-control-request-headers': 'x-access-token', accept: '*/*', 'accept-encoding': 'gzip, deflate, sdch, br', 'accept-language': 'en-us,en;q=0.8' } 

and so, sends response error 403

the service working correctly when sending request postman. how can fix problem?

you should try use beforesend hook jquery

 $.ajax({   url: "http://localhost:3000/api/books",   type: "get",   beforesend: function (xhr) {     xhr.setrequestheader('x-access-token', token);   },   success: function (datastring) {     console.log(json.stringify(datastring));   }, error: function (error) {     console.log(json.stringify(error));   } }); 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -