File size: 1,166 Bytes
6778ee0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
export function sendRequest(endpoint, payload, options) {
  if (COMPILE_COMPAT) {
    var request = new XMLHttpRequest()
    request.open('POST', endpoint, true)
    request.setRequestHeader('Content-Type', 'text/plain')

    request.send(JSON.stringify(payload))

    request.onreadystatechange = function () {
      if (request.readyState === 4) {
        if (request.status === 0) {
          options &&
            options.callback &&
            options.callback({ error: new Error('Network error') })
        } else {
          options &&
            options.callback &&
            options.callback({ status: request.status })
        }
      }
    }
  } else {
    if (window.fetch) {
      fetch(endpoint, {
        method: 'POST',
        headers: {
          'Content-Type': 'text/plain'
        },
        keepalive: true,
        body: JSON.stringify(payload)
      })
        .then(function (response) {
          options &&
            options.callback &&
            options.callback({ status: response.status })
        })
        .catch(function (error) {
          options && options.callback && options.callback({ error })
        })
    }
  }
}