Search code examples
djangoapiember.jscorsdjango-cors-headers

Not able to call my api throwing error Disallowed for cross-origin requests that require preflight


Hey I am new to ember and frontend thing.My backend is designed using django.All my api endpoint ends with " / "(ex:http://example.com/api/feed/).Now when I call this api's from ember app.It send the requests to "http://example.com/api/feed" and leaves the end " / ".

some codes snippet are:

routes.js:

return this.store.findRecord('feed');

model name is feed.

adapter.js:

host:"http://example.com/api",
headers:{'authorization:"abc"}

THE ERROR IN CONSOLE IS:

The request was redirected to 'http://example.com/api/feed/', which is disallowed for cross-origin requests that require preflight.

So it is redirecting to correct api end point but cors is not allowing the requests to happen.

Is there a way to call the api with " / "at end through ember.


Solution

  • Override your buildURL method in your adapter. Example for RESTAdapter:

    DS.RESTAdapter.extend({
      buildURL (modelName, id, snapshot, requestType, query) {
        return this._super(modelName, id, snapshot, requestType, query) + '/';
      }
    })
    

    You could probably also refactor this to use arguments instead of explicitly specyfing each param.