Start a new topic

How to get authentication using Javascript fetch?

Hi all,


I am trying to get all subscriptions using fetch. Here is my code:


const url = 'https://[mywebsite].chargebee.com/api/v2/subscriptions';

const username = [mysecrectkey];


fetch(url, 

      {method:'GET',

       headers: {'Authorization': 'Basic '+btoa(username)}

      })

      .then(function(res){

        return res.json();

      })

      .then(function(data) {

        console.log(data);

      });



But I get the error message like this:


Failed to load resource: the server responded with a status of 405 (Method Not Allowed)


Failed to load https://***.chargebee.com/api/v2/subscriptions: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:59987' is therefore not allowed access. The response had HTTP status code 405. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.


Uncaught (in promise) TypeError: Failed to fetch


anyone knows how to code it the right way? Thanks...

1 Comment

Hi Rico


Currently, we don’t support client-side authentication(CORS). So you need to proxy your request via a server(ex: node etc) for the code to function.


Let us know if you need further details.


Login or Signup to post a comment