2016-09-23 02:39:36 +08:00
|
|
|
import getUrl from 'discourse-common/lib/get-url';
|
2016-08-26 01:14:56 +08:00
|
|
|
|
|
|
|
let token;
|
|
|
|
|
2016-09-09 04:58:07 +08:00
|
|
|
export function getToken() {
|
2016-08-26 01:14:56 +08:00
|
|
|
if (!token) {
|
|
|
|
token = $('meta[name="csrf-token"]').attr('content');
|
|
|
|
}
|
|
|
|
|
2016-09-09 04:58:07 +08:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ajax(args) {
|
2016-08-26 01:14:56 +08:00
|
|
|
return new Ember.RSVP.Promise((resolve, reject) => {
|
2016-09-09 04:58:07 +08:00
|
|
|
args.headers = { 'X-CSRF-Token': getToken() };
|
2016-08-26 01:14:56 +08:00
|
|
|
args.success = data => Ember.run(null, resolve, data);
|
|
|
|
args.error = xhr => Ember.run(null, reject, xhr);
|
2016-09-23 02:39:36 +08:00
|
|
|
args.url = getUrl(args.url);
|
2016-08-26 01:14:56 +08:00
|
|
|
Ember.$.ajax(args);
|
|
|
|
});
|
|
|
|
}
|