/
run-request.js
51 lines (48 loc) · 1.46 KB
/
run-request.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import codeMirror from "./editor";
import runnerConfig from "./config-utils";
import "codemirror/lib/codemirror.css";
function runRequest() {
return new Promise(function (resolve, reject) {
var req = {
source: codeMirror.getValue(),
language: runnerConfig.getSelectedLanguage(),
};
let xhr = new XMLHttpRequest();
const fullUrl = runnerConfig.url + runnerConfig.runEndpoint;
xhr.open("POST", fullUrl);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onload = function () {
if (this.status >= 200 && this.status < 400) {
resolve(xhr.response);
} else {
reject({
status: this.status,
statusText: xhr.statusText,
});
}
};
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText,
});
};
xhr.send(JSON.stringify(req));
});
}
async function runCall() {
await runRequest()
.then(function (result) {
let out = JSON.parse(result);
document.getElementById("stdout-field").innerHTML =
"Stdout: " + out["stdout"].replace(/\n/g, "<br>");
document.getElementById("stderr-field").innerHTML =
"Stderr: " + out["stderr"];
document.getElementById("err-field").innerHTML = "Error: " + out["error"];
})
.catch(function (err) {
console.log(err);
document.getElementById("output-field").textContent = "Error: " + err;
});
}
export default runCall;