Skip to content

Instantly share code, notes, and snippets.

@diamondo25
Created November 25, 2016 14:28
Show Gist options
  • Save diamondo25/fcc5b4401af64bea5d110897bb916fbc to your computer and use it in GitHub Desktop.
Save diamondo25/fcc5b4401af64bea5d110897bb916fbc to your computer and use it in GitHub Desktop.
Follow Location header in Ember
// app/adapters/application.js
import DS from 'ember-data';
// Note: In order to allow the Location header to be seen through CORS requests,
// include 'Access-Control-Expose-Headers: Location' header in your API response.
export default DS.RESTAdapter.extend({
_ajaxRequest(hash) {
let originalSuccess = hash.success;
let originalError = hash.error;
hash.success = (payload, textStatus, jqXHR) => {
let locationHeader = jqXHR.getResponseHeader('Location');
if (locationHeader) {
this.ajax(locationHeader, 'GET').then((response) => {
originalSuccess(response, 'ok', jqXHR);
});
}
else {
originalSuccess(payload, textStatus, jqXHR);
}
};
// error is thrown in cases where it tried to parse the content,
// but should've followed the Location header instead
hash.error = (jqXHR, textStatus, errorThrown) => {
let locationHeader = jqXHR.getResponseHeader('Location');
if (locationHeader) {
this.ajax(locationHeader, 'GET').then((response) => {
originalSuccess(response, 'ok', jqXHR);
});
}
else {
originalError(jqXHR, textStatus, errorThrown);
}
};
return this._super(...arguments);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment