discourse/app/assets/javascripts/wizard/lib/ajax.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

34 lines
721 B
JavaScript
Raw Normal View History

import { Promise } from "rsvp";
import getUrl from "discourse-common/lib/get-url";
import jQuery from "jquery";
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 = document.querySelector('meta[name="csrf-token"]')?.content;
2016-08-26 01:14:56 +08:00
}
2016-09-09 04:58:07 +08:00
return token;
}
export function ajax(args) {
let url;
if (arguments.length === 2) {
url = arguments[0];
args = arguments[1];
} else {
url = args.url;
}
return new Promise((resolve, reject) => {
2016-09-09 04:58:07 +08:00
args.headers = { "X-CSRF-Token": getToken() };
args.success = (data) => run(null, resolve, data);
args.error = (xhr) => run(null, reject, xhr);
args.url = getUrl(url);
jQuery.ajax(args);
2016-08-26 01:14:56 +08:00
});
}