api.js 761 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * @description This module communicates with Lychee's API
  3. * @copyright 2015 by Tobias Reich
  4. */
  5. api = {
  6. path : 'php/index.php',
  7. onError : null
  8. }
  9. api.post = function(fn, params, callback) {
  10. loadingBar.show()
  11. params = $.extend({ function: fn }, params)
  12. const success = (data) => {
  13. setTimeout(loadingBar.hide, 100)
  14. // Catch errors
  15. if (typeof data==='string' && data.substring(0, 7)==='Error: ') {
  16. api.onError(data.substring(7, data.length), params, data)
  17. return false
  18. }
  19. callback(data)
  20. }
  21. const error = (jqXHR, textStatus, errorThrown) => {
  22. api.onError('Server error or API not found.', params, errorThrown)
  23. }
  24. $.ajax({
  25. type: 'POST',
  26. url: api.path,
  27. data: params,
  28. dataType: 'json',
  29. success,
  30. error
  31. })
  32. }