2019-11-08 13:28:11 -05:00
|
|
|
import { Promise } from "rsvp";
|
2016-09-22 14:39:36 -04:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2019-10-23 13:25:25 -04:00
|
|
|
import jQuery from "jquery";
|
2019-10-30 09:48:24 -04:00
|
|
|
import { run } from "@ember/runloop";
|
2016-08-25 13:14:56 -04:00
|
|
|
|
|
|
|
let token;
|
|
|
|
|
2016-09-08 16:58:07 -04:00
|
|
|
export function getToken() {
|
2016-08-25 13:14:56 -04:00
|
|
|
if (!token) {
|
|
|
|
token = $('meta[name="csrf-token"]').attr("content");
|
|
|
|
}
|
|
|
|
|
2016-09-08 16:58:07 -04:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function ajax(args) {
|
2019-11-08 13:28:11 -05:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-09-08 16:58:07 -04:00
|
|
|
args.headers = { "X-CSRF-Token": getToken() };
|
2019-10-30 09:48:24 -04:00
|
|
|
args.success = (data) => run(null, resolve, data);
|
|
|
|
args.error = (xhr) => run(null, reject, xhr);
|
2016-09-22 14:39:36 -04:00
|
|
|
args.url = getUrl(args.url);
|
2019-10-23 13:25:25 -04:00
|
|
|
jQuery.ajax(args);
|
2016-08-25 13:14:56 -04:00
|
|
|
});
|
|
|
|
}
|