HttpClient.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. function HttpClient()
  2. {
  3. try
  4. {
  5. var objRequest = new XMLHttpRequest();
  6. //var objRequest = new XDomainRequest();
  7. if (null != objRequest) this.m_objHttpRequest = objRequest;
  8. }
  9. catch (e)
  10. {
  11. try
  12. {
  13. var objRequest = new ActiveXObject("Msxml2.XMLHTTP");
  14. if (null != objRequest) this.m_objHttpRequest = objRequest;
  15. }
  16. catch (e)
  17. {
  18. try
  19. {
  20. var objRequest = new ActiveXObject("Microsoft.XMLHTTP");
  21. if (null != objRequest) this.m_objHttpRequest = objRequest;
  22. }
  23. catch (e)
  24. {
  25. throw new Error("HttpRequest not supported");
  26. }
  27. }
  28. }
  29. }
  30. HttpClient.prototype.send = function (strURL, objData, bAsync, handleASyncHttpResponse)
  31. {
  32. if (bAsync)
  33. {
  34. this.m_objHttpRequest.onreadystatechange = handleASyncHttpResponse;
  35. }
  36. else
  37. {
  38. this.m_objHttpRequest.onreadystatechange = null;
  39. }
  40. this.m_objHttpRequest.open("POST", strURL, bAsync);
  41. this.m_objHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  42. this.m_objHttpRequest.send(objData);
  43. if (!bAsync)
  44. {
  45. if (4 == this.m_objHttpRequest.readyState)
  46. {
  47. if (200 == this.m_objHttpRequest.status)
  48. {
  49. return this.m_objHttpRequest.responseText;
  50. }
  51. else
  52. {
  53. throw new Error(this.m_objHttpRequest.status);
  54. }
  55. }
  56. return this._handleSyncHttpResponse();
  57. }
  58. }