From 6e79e1fa0575c41821e8a4167d2b4bae42001a84 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Tue, 12 Oct 2021 12:59:54 +0100 Subject: [PATCH] replace jquery load with fetch (#828) * initial implementation * working with Dancer is_ajax * better network errors --- share/public/javascripts/netdisco.js | 67 +++++++++++++++++++++------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/share/public/javascripts/netdisco.js b/share/public/javascripts/netdisco.js index 1a5030c6..6018997b 100644 --- a/share/public/javascripts/netdisco.js +++ b/share/public/javascripts/netdisco.js @@ -30,29 +30,64 @@ function do_search (event, tab) { ); // submit the query and put results into the tab pane - $(target).load( uri_base + '/ajax/content/' + path + '/' + tab + '?' + query, - function(response, status, xhr) { - if (status !== "success") { + fetch( uri_base + '/ajax/content/' + path + '/' + tab + '?' + query, + { headers: { 'X-Requested-With': 'XMLHttpRequest' } }) + .then( response => { + if (! response.ok) { $(target).html( '
' + - 'Search failed! Please contact your site administrator.
' + 'Search failed! Please contact your site administrator (server error).' ); return; + // throw new Error('Network response was not ok'); } - if (response == "") { - $(target).html( - '
No matching records.
' - ); + return response.text(); + }) + .then( content => { + if (content == "") { + $(target).html('
No matching records.
'); } + else { + $(target).html(content); + // delegate to any [device|search] specific JS code + $('div.content > div.tab-content table.nd_floatinghead').floatThead({ + scrollingTop: 40 + ,useAbsolutePositioning: false + }); + inner_view_processing(tab); + } + }) + .catch( error => { + $(target).html( + '
' + + 'Search failed! Please contact your site administrator (network error: ' + error + ').
' + ); + console.error('There has been a problem with your fetch operation:', error); + }); - // delegate to any [device|search] specific JS code - $('div.content > div.tab-content table.nd_floatinghead').floatThead({ - scrollingTop: 40 - ,useAbsolutePositioning: false - }); - inner_view_processing(tab); - } - ); +// $(target).load( uri_base + '/ajax/content/' + path + '/' + tab + '?' + query, +// function(response, status, xhr) { +// if (status !== "success") { +// $(target).html( +// '
' + +// 'Search failed! Please contact your site administrator.
' +// ); +// return; +// } +// if (response == "") { +// $(target).html( +// '
No matching records.
' +// ); +// } +// +// // delegate to any [device|search] specific JS code +// $('div.content > div.tab-content table.nd_floatinghead').floatThead({ +// scrollingTop: 40 +// ,useAbsolutePositioning: false +// }); +// inner_view_processing(tab); +// } +// ); } // keep track of which tabs have a sidebar, for when switching tab