javascript - How to disable 'withCredentials' in api call using node.js only (https package) -


i'm using node.js script load in-built https package. when using error:

xmlhttprequest cannot load [constructed-api-url]. wildcard '*' cannot used in 'access-control-allow-origin' header when credentials flag true. origin 'http://localhost:3000' therefore not allowed access. credentials mode of xmlhttprequest controlled withcredentials attribute.

i'm using node.js 4.4.3, , https api docs not mention withcredentials.

the script being used this one.

is there anyway set xhr call's withcredentials false using node.js https?

i looking analogous jquery ajax call (just focusing on xhr field):

$.ajax({             type: 'post',  async:true,             url: 'https://someapp.constructed.url/token',             datatype: "json",             contenttype: 'application/x-www-form-urlencoded; charset=utf-8',             xhrfields: {                 withcredentials: true             },             headers: {                 'authorization': 'basic ' + appinfo                                 },                           success: function (result) {                 var token = result.access_token;                 //…                                },             error: function (req, status, error) {                 if (typeof(req) != 'undefined') {                     var msg = status || req.responsejson.error;                     //…                 }                                }     }); 

there similar example, related request package, don't want include in dependencies. beside, used script using https.

so answer there time, after all:

after bit of research, found node's https package uses practically same options http, including withcredentials option (not documented in node's http/https, part of xhr documentation). matter of including url option within options object along withcredentials option, , pass options object parameter https.get.

and constructed code more or less follows (focus on options variable):

var options = {   url: 'https://my.domain.com/api/endpoint',   withcredentials: false   } var querystring = '?key=' + [_my_api_key]; var param1 = '&' + [paramkey] + '=' + [paramvalue];  var datos;  options.url += querystring; options.url += param1;  https.get(options, function (res) {   res.on('data', function (data) {     datos += data;   });    res.on('end', function () {           try {       var data = json.parse(datos);     } catch (e) {       console.error('unable parse response json', e);     }   }); }).on('error', function (e) {     console.error('an error occurred request:', e.message);     callback(e.message); }); 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -