Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hassansardarbangash opened this issue Nov 4, 2021 · 0 comments
Open

Comments

@hassansardarbangash
Copy link

hassansardarbangash commented Nov 4, 2021

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?

@hassansardarbangash hassansardarbangash changed the title CORS request did not succeed Reason: CORS header ‘Access-Control-Allow-Origin’ missing Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant