-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathpost_json.js
55 lines (52 loc) · 1.81 KB
/
post_json.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
52
53
54
55
// Send an HTTP request to the server with POSTed json-data
// This is adapted from https://stackoverflow.com/questions/24468459/sending-a-json-to-server-and-retrieving-a-json-in-return-without-jquery
function post_json(network_data, config_data, canvas, callback)
{
let xhr = new XMLHttpRequest();
let url = window.location.href;
let img = canvas.toDataURL("image/png");
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let response = xhr.responseText;
console.log(response);
if (callback !== null)
callback();
}
};
let joint_data = { 'network':network_data, 'config':config_data, 'image':img};
let data = JSON.stringify(joint_data);
xhr.send(data);
}
function post_stop()
{
let xhr = new XMLHttpRequest();
let url = window.location.href;
let this_close_function = window.close;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let response = xhr.responseText;
console.log(response);
this_close_function();
}
};
xhr.send();
}
window.addEventListener("unload", post_window_closed_stop);
function post_window_closed_stop()
{
let xhr = new XMLHttpRequest();
let url = window.location.href;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
let response = xhr.responseText;
console.log(response);
}
};
xhr.send();
}