api.js 724 B

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