Created
November 25, 2016 14:28
-
-
Save diamondo25/fcc5b4401af64bea5d110897bb916fbc to your computer and use it in GitHub Desktop.
Follow Location header in Ember
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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