comparison Platforms/Wasm/WasmWebService.js @ 309:14ef1227120f am-callable-and-promise

web services: better handling of failures
author am@osimis.io
date Fri, 28 Sep 2018 15:02:43 +0200
parents ed1a4302154f
children aad37d0b6407
comparison
equal deleted inserted replaced
307:be2660b6e40a 309:14ef1227120f
1 mergeInto(LibraryManager.library, { 1 mergeInto(LibraryManager.library, {
2 WasmWebService_GetAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload) { 2 WasmWebService_GetAsync: function(callableSuccess, callableFailure, url, headersInJsonString, payload, timeoutInSeconds) {
3 // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data 3 // Directly use XMLHttpRequest (no jQuery) to retrieve the raw binary data
4 // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/ 4 // http://www.henryalgus.com/reading-binary-files-using-jquery-ajax/
5 var xhr = new XMLHttpRequest(); 5 var xhr = new XMLHttpRequest();
6 var url_ = UTF8ToString(url); 6 var url_ = UTF8ToString(url);
7 var headersInJsonString_ = UTF8ToString(headersInJsonString); 7 var headersInJsonString_ = UTF8ToString(headersInJsonString);
8 8
9 xhr.open('GET', url_, true); 9 xhr.open('GET', url_, true);
10 xhr.responseType = 'arraybuffer'; 10 xhr.responseType = 'arraybuffer';
11 xhr.timeout = timeoutInSeconds * 1000;
11 var headers = JSON.parse(headersInJsonString_); 12 var headers = JSON.parse(headersInJsonString_);
12 for (var key in headers) { 13 for (var key in headers) {
13 xhr.setRequestHeader(key, headers[key]); 14 xhr.setRequestHeader(key, headers[key]);
14 } 15 }
15 //console.log(xhr); 16 //console.log(xhr);
28 } 29 }
29 30
30 xhr.send(); 31 xhr.send();
31 }, 32 },
32 33
33 WasmWebService_PostAsync: function(callableSuccess, callableFailure, url, headersInJsonString, body, bodySize, payload) { 34 WasmWebService_PostAsync: function(callableSuccess, callableFailure, url, headersInJsonString, body, bodySize, payload, timeoutInSeconds) {
34 var xhr = new XMLHttpRequest(); 35 var xhr = new XMLHttpRequest();
35 var url_ = UTF8ToString(url); 36 var url_ = UTF8ToString(url);
36 var headersInJsonString_ = UTF8ToString(headersInJsonString); 37 var headersInJsonString_ = UTF8ToString(headersInJsonString);
37 xhr.open('POST', url_, true); 38 xhr.open('POST', url_, true);
39 xhr.timeout = timeoutInSeconds * 1000;
38 xhr.responseType = 'arraybuffer'; 40 xhr.responseType = 'arraybuffer';
39 xhr.setRequestHeader('Content-type', 'application/octet-stream'); 41 xhr.setRequestHeader('Content-type', 'application/octet-stream');
40 42
41 var headers = JSON.parse(headersInJsonString_); 43 var headers = JSON.parse(headersInJsonString_);
42 for (var key in headers) { 44 for (var key in headers) {