Seif Sallam
Seif Sallam

Reputation: 831

Using Ember-data RESTAdapter with different URL

I'm using ember data with different URL as i'm moving from ember-rails, to external ember application, so API url is http://localhost:5000 and ember application is http://localhost:9000.

Now the problem is I need to include x-appid and x-app-secret, but whenever add xhr.setRequestHeader() for any of them, the GET request becomes OPTIONS request.

This code was working fine when i was using ember-rails on the same domain, is this the problem or is there something else missing?

ajax: function(url, type, hash) {
    if (this.headers !== undefined) {
      var headers = this.headers;
      if (hash) {
        hash.beforeSend = function (xhr) {
          // Works fine
          xhr.setRequestHeader('Accept', 'application/vnd.vendor+json;version=1');

          // Changes Request from GET to OPTIONS
          xhr.setRequestHeader('x-vendor-appid', '12412412');
          xhr.setRequestHeader('x-vendor-secret', 'aslkdfjaskldfjasd');
        };
      }
    }
    return this._super(url, type, hash);
}

Upvotes: 0

Views: 420

Answers (1)

buuda
buuda

Reputation: 1427

The browser is sending an OPTIONS request because the domain of the request does not match the domain of your page. This triggers Cross-Origin Resource Sharing (CORS) restrictions.

You need to respond to the CORS request from the server to tell the client it is allowed to make CORS requests to the domain by setting a few HTTP header fields. The browser will then make the original request. I have implemented this with ember.js/rails.

Upvotes: 1

Related Questions