-
Notifications
You must be signed in to change notification settings - Fork 391
/
orderPlaced.js
27 lines (24 loc) · 908 Bytes
/
orderPlaced.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
document.cookie = "orderId="+0 +",counter="+0
let httpRequest = new XMLHttpRequest(),
jsonArray,
method = "GET",
jsonRequestURL = "https://5d76bf96515d1a0014085cf9.mockapi.io/order";
httpRequest.open(method, jsonRequestURL, true);
httpRequest.onreadystatechange = function()
{
if(httpRequest.readyState == 4 && httpRequest.status == 200)
{
// convert JSON into JavaScript object
jsonArray = JSON.parse(httpRequest.responseText)
console.log(jsonArray)
jsonArray.push(
{
"id": (jsonArray.length)+1, "amount": 200,"product":["userOrder"]
})
// send with new request the updated JSON file to the server:
httpRequest.open("POST", jsonRequestURL, true)
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
httpRequest.send(jsonArray)
}
}
httpRequest.send(null);