# Ember-Data and CORS

**URL:** <https://discuss.emberjs.com/t/ember-data-and-cors/3690>\
**Category:** Ember Data\
**Created:** [December 27, 2013, 11:23pm UTC](https://discuss.emberjs.com/t/ember-data-and-cors/3690 "2013-12-27T23:23:14Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![mixonic](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mixonic/32/14829_2.png) [@mixonic](https://discuss.emberjs.com/u/mixonic)\
**Post date:** [December 28, 2013, 6:08am UTC](https://discuss.emberjs.com/t/ember-data-and-cors/3690/2 "2013-12-28T06:08:47Z")

</div>

Howdy Andrew.

Ember-Data has no special support for CORS, but since Ember-Data is composed of flexible primitives adding support to your application should be simple. For instance adding CORS support to a single endpoint is pretty easy:

```nohighlight
App.ThingAdapter = DS.RESTAdapter.extend({
  findAll: function(store, type, id) {
    return this.ajax("https://cors-test.appspot.com/test", "GET", {
      // CORS
      crossDomain: true,
      xhrFields: {withCredentials: true}
    }).then(function(json) {
      // Massage this demo API endpoint to look like RESTAdapter expects.
      return { things: [json] };
    });
  }
});

```

A fiddle: [http://emberjs.jsbin.com/osULomo/2/edit?html,js,output](http://emberjs.jsbin.com/osULomo/2/edit?html,js,output)

The `ajax` method called above is just a promise-returning wrapper around jQuery’s XHR. You could also add CORS and a custom host to all models:

```nohighlight
App.ApplicationAdapter = DS.RESTAdapter.extend({
  host: 'https://otherhost.com',
  ajax: function(url, method, hash) {
    hash.crossDomain = true;
    hash.xhrFields = {withCredentials: true};
    return this._super(url, method, hash);
  }
});

```

Again, a demo: [http://emberjs.jsbin.com/osULomo/3/edit?html,js,output](http://emberjs.jsbin.com/osULomo/3/edit?html,js,output)

Ember-Data is currently best viewed (imo, and hopefully not forever), as a great set of primitives. All the extension points for something like CORS are present, if a little under-documented.

Of course, I personally use an `ajaxPrefilter` call to handle this in several apps which is yet another way to tackle CORS. There really are plenty of options, and just because you use Ember-Data doesn’t mean you need to ignore jQuery APIs.

---

_[View the full topic](https://discuss.emberjs.com/t/ember-data-and-cors/3690)._
