Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access-Control-Allow-Origin error when using ember.js(with ember-cli)

Here is my routes in (app/routes/customers.js):

export default Ember.Route.extend({
  model: function() {
    return $.getJSON("http://127.0.0.1:3000/odata/customers");
  }
});

here is my router.js:

export default Router.map(function() {
  this.route('customers', {path: '/'});
});

http://127.0.0.1:3000/odata/customers is my api, but ember-cli use http://localhost:4200/, when I open http://localhost:4200/

in the console, the error message is: XMLHttpRequest cannot load http://127.0.0.1:3000/odata/customers. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

I find a article: https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

so I know what's wrong, but I don't kown how to fix it when use ember.js.

sorry for my poor English, hope thats clear...

like image 362
ChiangDi Avatar asked May 10 '15 03:05

ChiangDi


People also ask

How do I fix Access-Control allow origin?

Since the header is currently set to allow access only from https://yoursite.com , the browser will block access to the resource and you will see an error in your console. Now, to fix this, change the headers to this: res. setHeader("Access-Control-Allow-Origin", "*");

How do you allow cross origin?

Allow CORS: Access-Control-Allow-Origin lets you easily perform cross-domain Ajax requests in web applications. Simply activate the add-on and perform the request. CORS or Cross-Origin Resource Sharing is blocked in modern browsers by default (in JavaScript APIs).

Is not allowed by Access-Control allow origin?

This error occurs when a script on your website/web app attempts to make a request to a resource that isn't configured to accept requests coming from code that doesn't come from the same (sub)domain, thus violating the Same-Origin policy.

Is Access-Control allow Origin * Insecure?

Access-Control-Allow-Origin: * is totally safe to add to any resource, unless that resource contains private data protected by something other than standard credentials. Standard credentials are cookies, HTTP basic auth, and TLS client certificates.


1 Answers

It's not Ember that is the problem. it's the server at port 3000. If your Ember app is running on a different port, it is basically cross domain and thus the server at port 3000 must be CORS enabled. Example of this is like this for node js and express: http://enable-cors.org/server_expressjs.html You need to figure out how to do this for your back end. But what it comes down to is just basically adding the right headers into the response stream.

Some other example:

http://enable-cors.org/server_iis7.html

http://enable-cors.org/server_php.html

Note that if you do want to prevent the Ember server from sending requests to other origins, you can use Content Security Policies.

like image 122
Jimmy Chandra Avatar answered Oct 07 '22 23:10

Jimmy Chandra