nandanself
nandanself

Reputation: 835

Not able to make request to backend server in ember (error :'No Access-Control-Allow-Origin')

I have design adapter in ember js which send requests to backend to get the data but getting a error "XMLHttpRequest cannot load https://example.com/api. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access." in console.

I can solve the problem if I use the following code in nginx config file on my server.The backend is designed using django.

if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }

But I don't want to disturb the tha nginx config.

adapter

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
    host:"https://example.com/api/",
});

How to send the request so that it doesn't show any error

Upvotes: 0

Views: 397

Answers (2)

dustinfarris
dustinfarris

Reputation: 1370

Configure this using django-cors-headers in your settings.py:

CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
  'x-requested-with',
  'content-type',
  'accept',
  'origin',
  'authorization',
  'x-csrftoken',
  'cache-control',
  'accept-encoding',
)

Upvotes: 1

Mario Pabon
Mario Pabon

Reputation: 605

Unfortunately, you're going to have to edit your server's config file. Otherwise, you would have to serve the app from the same domain and port as your back-end.

Upvotes: 1

Related Questions