2019-11-09 02:28:11 +08:00
|
|
|
import { Promise } from "rsvp";
|
2016-09-23 02:39:36 +08:00
|
|
|
import getUrl from "discourse-common/lib/get-url";
|
2019-10-24 01:25:25 +08:00
|
|
|
import jQuery from "jquery";
|
2019-10-30 21:48:24 +08:00
|
|
|
import { run } from "@ember/runloop";
|
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) {
|
2021-07-21 19:49:21 +08:00
|
|
|
let url;
|
|
|
|
|
|
|
|
if (arguments.length === 2) {
|
|
|
|
url = arguments[0];
|
|
|
|
args = arguments[1];
|
|
|
|
} else {
|
|
|
|
url = args.url;
|
|
|
|
}
|
|
|
|
|
2019-11-09 02:28:11 +08:00
|
|
|
return new Promise((resolve, reject) => {
|
2016-09-09 04:58:07 +08:00
|
|
|
args.headers = { "X-CSRF-Token": getToken() };
|
2019-10-30 21:48:24 +08:00
|
|
|
args.success = (data) => run(null, resolve, data);
|
|
|
|
args.error = (xhr) => run(null, reject, xhr);
|
2021-07-21 19:49:21 +08:00
|
|
|
args.url = getUrl(url);
|
2019-10-24 01:25:25 +08:00
|
|
|
jQuery.ajax(args);
|
2016-08-26 01:14:56 +08:00
|
|
|
});
|
|
|
|
}
|