fix not working InfoLog index: synchronous request must return fulfilled promise

This commit is contained in:
Ralf Becker 2021-07-16 11:49:53 +02:00
parent b780c7ebd8
commit 50b8839609
2 changed files with 20 additions and 18 deletions

View File

@ -70,7 +70,8 @@ export function et2_loadXMLFromURL(_url : string, _callback? : Function, _contex
return xmldoc.children[0]; return xmldoc.children[0];
}) })
.catch((_err) => { .catch((_err) => {
egw().debug('error', 'Loading eTemplate from '+_url+' failed! '+_err.status+' '+_err.statusText); egw().message('Loading eTemplate from '+_url+' failed!'+"\n\n"+
(typeof _err.stack !== 'undefined' ? _err.stack : _err.status+' '+_err.statusText), 'error');
if(typeof _fail_callback === 'function') { if(typeof _fail_callback === 'function') {
_fail_callback.call(_context, _err); _fail_callback.call(_context, _err);
} }

View File

@ -75,9 +75,6 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
this.sender = _sender ? _sender : null; this.sender = _sender ? _sender : null;
this.egw = _egw; this.egw = _egw;
// We currently don't have a request object
this.request = null;
// Some variables needed for notification about a JS files done loading // Some variables needed for notification about a JS files done loading
this.onLoadFinish = null; this.onLoadFinish = null;
this.jsFiles = 0; this.jsFiles = 0;
@ -191,7 +188,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
* @param {string} method ='POST' allow to eg. use a (cachable) 'GET' request instead of POST * @param {string} method ='POST' allow to eg. use a (cachable) 'GET' request instead of POST
* @param {function} error option error callback(_xmlhttp, _err) used instead our default this.error * @param {function} error option error callback(_xmlhttp, _err) used instead our default this.error
* *
* @return {jqXHR|boolean} jQuery jqXHR request object or for async==="keepalive" boolean is returned * @return {Promise|boolean} Promise or for async==="keepalive" boolean is returned
*/ */
json_request.prototype.sendRequest = function(async, method, error) json_request.prototype.sendRequest = function(async, method, error)
{ {
@ -209,7 +206,7 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
} }
}); });
// send with keepalive===true or sendBeacon to be used in beforeunload event // send with keepalive===true for sendBeacon to be used in beforeunload event
if (this.async === "keepalive" && typeof navigator.sendBeacon !== "undefined") if (this.async === "keepalive" && typeof navigator.sendBeacon !== "undefined")
{ {
const data = new FormData(); const data = new FormData();
@ -231,16 +228,17 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
init.headers = { 'Content-Type': 'application/json'}; init.headers = { 'Content-Type': 'application/json'};
init.body = request_obj; init.body = request_obj;
} }
let promise;
if (this.async) if (this.async)
{ {
this.request = (this.egw.window?this.egw.window:window).fetch(url, init) promise = (this.egw.window?this.egw.window:window).fetch(url, init)
.then((response) => { .then((response) => {
if (!response.ok) { if (!response.ok || !response.headers.get('Content-Type').startsWith('application/json')) {
throw response; throw response;
} }
return response.json(); return response.json();
}) })
.then((data) => this.handleResponse(data)) .then((data) => this.handleResponse(data) || data)
.catch((_err) => { .catch((_err) => {
(error || this.handleError).call(this, _err) (error || this.handleError).call(this, _err)
}); });
@ -248,20 +246,24 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
else else
{ {
console.trace("Synchronous AJAX request detected", this); console.trace("Synchronous AJAX request detected", this);
this.request = new XMLHttpRequest(); const request = new XMLHttpRequest();
this.request.open(method, url, false); request.open(method, url, false);
if (method !== 'GET') this.request.setRequestHeader('Content-Type', 'application/json'); if (method !== 'GET') request.setRequestHeader('Content-Type', 'application/json');
this.request.send(init.body); request.send(init.body);
if (this.request.status !== 200) if (request.status >= 200 && request.status < 300)
{ {
(error || this.handleError).call(this, this.request, 'error') const json = JSON.parse(request.responseText);
promise = Promise.resolve(this.handleResponse(json) || json);
} }
else else
{ {
this.handleResponse(JSON.parse(this.request.responseText)); (error || this.handleError).call(this, request, 'error')
} }
} }
return this.request; // compatibility with jQuery.ajax
if (promise && typeof promise.then === 'function') promise.done = promise.then;
return promise;
}; };
/** /**
@ -419,7 +421,6 @@ egw.extend('json', egw.MODULE_WND_LOCAL, function(_app, _wnd)
this.callback.call(this.context,res); this.callback.call(this.context,res);
} }
} }
this.request = null;
}; };
var json = { var json = {