Newer
Older
backup-commander / html / js / common.js
  1.  
  2. /**
  3. * COPYRIGHT © 2022 JOHN PEARCEY
  4. * All rights reserved
  5. */
  6.  
  7. /*
  8. * pagename:
  9. * The name of the server-side script to handle this post.
  10. * fnCallback:
  11. * The javascript callback function, called after the AJAX call completes.
  12. * map_params:
  13. * A map containing name value pairs required by the script.
  14. * e.g.
  15. * map_params = new Map();
  16. map_params.set('username', document.getElementById('username').value );
  17. map_params.set('pwdhash', document.getElementById('pwd').value );
  18. */
  19. function postData( pagename, fnCallback, map_params ){
  20. //console.log( 'postData called' );
  21. let formData = new FormData();
  22. map_params.forEach((value, key, map) => {
  23. //if(key=='action') console.log("postData: ", pagename, value );
  24. formData.append( key, value );
  25. });
  26. //https://www.digitalocean.com/community/tutorials/how-to-use-the-javascript-fetch-api-to-get-data
  27. //https://jsonplaceholder.typicode.com/users
  28. //https://javascript.info/fetch
  29. fetch( pagename, {
  30. method: 'POST',
  31. body: formData,
  32. })
  33. /*.then((response) => {
  34. return response.json(); */
  35.  
  36. .then(response => response.text())
  37. .then((dataStr) => {
  38. //console.log(dataStr);
  39. //there's a whole bunch of warnings and errors that might be sent by PHP. It is not possible
  40. //to catch them in the serverside PHP code. So here we search for our json encoded return string which
  41. //must be either Error or OK.
  42. let pos = dataStr.indexOf("[\"OK\"");
  43. let additional=null;
  44. if(pos==-1){
  45. pos = dataStr.indexOf("[\"Error\"");
  46. if(pos==-1){
  47. //the PHP is pretty fucked up if you get here!
  48. console.warn( dataStr );
  49. let encodedStr = JSON.stringify( ["Illegal return from server. Did not find Error or OK.", dataStr ] );
  50. return JSON.parse( encodedStr );
  51. }
  52. }
  53. //console.log(dataStr);
  54. return JSON.parse(dataStr);
  55. }).then((data) => {
  56. fnCallback( true, data, map_params );
  57. }).catch(function(error) {
  58. console.log( error );
  59. fnCallback( false, error, map_params );
  60. });
  61. }
  62.  
  63. function checkReturn( success, data, bGetErr=false ){
  64.  
  65. if(!success){
  66. console.warn( "callback error: " + data );
  67. return false;
  68. }
  69.  
  70. if(data[0]=="Error"){
  71. if(bGetErr) return data;
  72. alert( "Error: " + data[1] );
  73. for( i=2; i<data.length; i++){
  74. console.warn( data[i] );
  75. }
  76. return false;
  77. }
  78.  
  79. if(data[0]!="OK"){
  80. console.warn( "Unkown server return string ", data );
  81. if(bGetErr) return data;
  82. return false;
  83. }
  84. if(bGetErr) return null;
  85. return true;
  86. }
  87.  
  88. function stripHtml( txt ){
  89. txt = txt.replace(/<div>/gi, '\n');
  90. txt = txt.replace(/<\/?[^>]+>/gi, ''); //strip HTML
  91. return txt;
  92. }