kahmali / meteor-restivus

REST APIs for the Best of Us! - A Meteor 0.9+ package for building REST APIs https://atmospherejs.com/nimble/restivus
MIT License
544 stars 117 forks source link

Reason: CORS header ‘Access-Control-Allow-Origin’ missing #312

Open hassansardarbangash opened 2 years ago

hassansardarbangash commented 2 years ago

I am facing issue calling Remote API from localhost.

I am getting this error in browser console:

Reason: CORS request did not succeed

I am using nimble:restivus package

https://github.com/kahmali/meteor-restivus

As per documentation I am setting:

enableCors: true

in order to allow access from any origin. But no luck.

Here is my server side code:

myApi = new Restivus({

    apiPath: 'api/',
    enableCors: true,
    useDefaultAuth: false,
    prettyJson: true,
    defaultOptionsEndpoint: {
        action: function() {
            this.response.writeHead(201, {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Credentials": "true",
                "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept, Z-Key, Authorization",
                "Content-Type": "application/json",
                "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS"
            });
            this.done();
            return {
                status: "success",
                "data": {
                    "message": "We love OPTIONS"
                }
            };
        }
    }
});

Here is my client side code:

$.ajax
        ({
          type: "POST",
          url: "https://example.com/api/getUsers",
          dataType: 'json',
          headers: {
            "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI"
          }
          data: {},
          success: function (){
            alert('Thanks for your comment!'); 
          }
        });

If I send nothing in header, the request succeed otherwise it gives error in browser console.

Can anyone help me with this?