Some experiments with web applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.7 KiB

  1. /**
  2. * Created with JetBrains WebStorm.
  3. * User: torsten
  4. * Date: 28.03.13
  5. * Time: 23:07
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. gcf = {
  9. // xmlhttp object function
  10. getHTTPObject : function () {
  11. // debug log
  12. if (DEBUG === 1) {console.log("xml http object function");}
  13. // variable definitions
  14. var xhr;
  15. // check for availibility if xmlhttprequest object
  16. if(window.XMLHttpRequest) {
  17. xhr = new XMLHttpRequest();
  18. } else if(window.ActiveXObject) {
  19. xhr = new ActiveXObject("Msxml2.XMLHTTP");
  20. }
  21. return xhr;
  22. },
  23. // ajax call function
  24. ajaxCall : function (dataUrl, outputElement, callback, responseType) {
  25. // debug log
  26. if (DEBUG === 1) {console.log("ajax function");}
  27. // variable definitions
  28. var response,
  29. request = this.getHTTPObject(); // get the xmlhttp object which is supported
  30. outputElement.innerHTML = "Lade Daten ...";
  31. request.onreadystatechange = function() {
  32. if(request.readyState === 4 && request.status === 200) {
  33. //save ajax response
  34. if (responseType === "json") {
  35. response = JSON.parse(request.responseText);
  36. } else if (responseType === "xml") {
  37. response = request.responseXML;
  38. } else {
  39. response = request.responseText;
  40. }
  41. // check if callback is a function
  42. if(typeof callback === "function") {
  43. callback(response);
  44. }
  45. }
  46. };
  47. request.open("get", dataUrl, true);
  48. request.send(null);
  49. }
  50. }