Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.post('/api/update', (req, res) => {
- res.send(req.body);
- });
- const HTTPClient = (method, url, post) => {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.open(method, url, true);
- xhr.onload = () => {
- const statusText = xhr.statusText || '';
- // responseText is the old-school way of retrieving response (supported by IE9)
- // response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
- const response = ('response' in xhr) ? xhr.response : xhr.responseText;
- // normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
- let status = xhr.status === 1223 ? 204 : xhr.status;
- // fix status code when it is 0 (0 status is undocumented).
- // Occurs when accessing file resources or on Android 4.1 stock browser
- // while retrieving files from application cache.
- if (status === 0) {
- status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
- }
- try {
- return resolve(JSON.parse(response));
- } catch (error) {
- return resolve(response);
- }
- };
- xhr.onerror = () => reject('Error');
- xhr.ontimeout = () => reject('Timeout');
- xhr.onabort = () => reject('Abort');
- xhr.send(JSON.stringify(post) || null);
- });
- };
- HTTPClient('post', '/api/update', defaultData).then(response => {
- css.value = response.data;
- console.log(response);
- }, error => {
- console.error(error);
- });
Add Comment
Please, Sign In to add comment