简体   繁体   中英

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

I'm using a node.js script that load in-built https package. When using it I get error:

XMLHttpRequest cannot load [constructed-api-url]. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:3000' is therefore not allowed access. The credentials mode of an XMLHttpRequest is controlled by the withCredentials attribute.

I'm using node.js 4.4.3, and its https api docs does not really mention anything about withCredentials.

The script being used is this one .

Is there anyway to set the xhr call's withCredentials to false using node.js https?

I am looking to something analogous to this jquery ajax call (just focusing on the 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 is another very similar example, but this is related to the request package, which I don't want to include in dependencies. Beside, my used script is already using https.

So the answer was there all the time, after all:

After a bit of research, found that node's https package uses practically same options as http, including the withCredentials option (not documented in node's http/https, but part of xhr documentation). It was a matter of including the url option within the options object along the withCredentials option, and then pass the options object as parameter for https.get .

And the constructed code would be more or less as follows (focus on the 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 to parse response as JSON', e);
    }
  });
}).on('error', function (e) {
    console.error('An error occurred with the request:', e.message);
    callback(e.message);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM