actionWorker.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. function V(httpRequest) {
  2. if (httpRequest.readyState === XMLHttpRequest.DONE) {
  3. if (httpRequest.status === 200) {
  4. alert(httpRequest.responseText);
  5. } else {
  6. alert('There was a problem with the request.');
  7. }
  8. }
  9. }
  10. (function(){
  11. var _worker = this;
  12. var _action = {
  13. _checkRequest: function(){
  14. if (this.request.readyState === XMLHttpRequest.DONE) {
  15. if (this.request.status === 200) {
  16. this._doneRequest();
  17. } else {
  18. this._errorRequest();
  19. }
  20. }
  21. },
  22. _createRequest: function(){
  23. this.request = new XMLHttpRequest();
  24. this.request.addEventListener("readystatechange", this._checkRequest.bind(this));
  25. },
  26. sendRequest: function(data){
  27. if (!this.request) this._createRequest();
  28. var method = data.method;
  29. var noCache = !!data.noCache;
  30. var async = !!data.loadAsync;
  31. var withCredentials = !!data.credentials;
  32. var url = data.address;
  33. var body = data.body;
  34. var debug = data.debug;
  35. var token = data.token;
  36. var tokenName = data.tokenName;
  37. if (noCache) url = url+(((url.indexOf("?")!==-1) ? "&" : "?")+(new Date()).getTime());
  38. this.request.open(method, url, async);
  39. this.request.withCredentials = withCredentials;
  40. this.request.setRequestHeader("Content-Type", "application/json; charset=utf-8");
  41. this.request.setRequestHeader("Accept", "text/html,application/json,*/*");
  42. if (debug) this.request.setRequestHeader("x-debugger", "true");
  43. if (token){
  44. this.request.setRequestHeader(tokenName, token);
  45. this.request.setRequestHeader("Authorization", token);
  46. }
  47. this.request.send(body);
  48. },
  49. _doneRequest: function(){
  50. var json = JSON.parse(this.request.responseText);
  51. var xToken = this.request.getResponseHeader(o2.tokenName);
  52. if (xToken){
  53. json.xToken = xToken;
  54. }
  55. _worker.postMessage({"type": "done", "data": json});
  56. _worker.close();
  57. },
  58. _errorRequest: function(){
  59. _worker.postMessage({"type":"error", "data": {"status": this.request.status, "statusText": this.request.statusText, "responseText":this.request.responseText}});
  60. _worker.close();
  61. }
  62. };
  63. this.action = _action;
  64. })();
  65. onmessage = function(e) {
  66. this.action.sendRequest(e.data);
  67. //
  68. //
  69. // console.log('Worker: Message received from main script');
  70. // var options = e.data;
  71. //
  72. // httpRequest = new XMLHttpRequest();
  73. //
  74. //
  75. // if (isNaN(result)) {
  76. // postMessage('Please write two numbers');
  77. // } else {
  78. // const workerResult = 'Result: ' + result;
  79. // console.log('Worker: Posting message back to main script');
  80. // postMessage(workerResult);
  81. // }
  82. }