HTTP Request

HTTP Request


Published in: JavaScript 


Generic global code for managing XMLHttpRequest objects.

 
  
  1. // ----------------------------------------
  2. // Wrapper function for constructing a request object.
  3. // Parameters:
  4. // reqType: The HTTP request type, such as GET or POST.
  5. // url: The URL of the server program.
  6. // asynch: Whether to send the request asynchronously or not.
  7. // ----------------------------------------
  8.  
  9. function httpRequest (reqType ,url ,asynch )  {
  10.  
  11. // Mozilla-based browsers
  12. if  (window. XMLHttpRequest )  {
  13. request  =  new XMLHttpRequest ( );
  14. }  else  if  (window. ActiveXObject )  {
  15. request  =  new ActiveXObject ( "Msxml2.XMLHTTP" );
  16. if  ( !request )  {
  17. request  =  new ActiveXObject ( "Microsoft.XMLHTTP" );
  18. }
  19. }
  20.  
  21. // Request could still be null if neither ActiveXObject
  22. // initialization succeeded
  23. if  (request )  {
  24. // If the reqType param is POST, then the fifth arg is the POSTed data
  25. if  (reqType. toLowerCase ( )  !=  "post" )  {
  26. initReq (reqType , url , asynch , respHandle );
  27. }  else  {
  28. // The POSTed data
  29. var args  = arguments [ 4 ];
  30. if  (args  !=  null  && args. length  > 0 )  {
  31. initReq (reqType , url , asynch , respHandle , args );
  32. }
  33. }
  34. }  else  {
  35. alert ( "Your browser does not permit the use of all "  +
  36. "of this application's features!" );
  37. }
  38.  
  39. }
  40.  
  41. // ----------------------------------------
  42. // Initialize a request object that is already constructed
  43. // ----------------------------------------
  44.  
  45. function initReq (reqType , url , bool , respHandle )  {
  46. try  {
  47. // Specify the function that will handle the HTTP response
  48. request. onreadystatechange  = handleResponse;
  49. request. open (reqType , url , bool );
  50. // If the reqType param is POST, then the
  51. // fifth argument to the function is the POSTed data
  52. if  (reqType. toLowerCase ( )  ==  "post" )  {
  53. // Set the Content-Type header for a POST request
  54. request. setRequestHeader ( "Content-Type" ,  "application/x-ww-form-urlencoded; charset=UTF-8" );
  55. request. send (arguments [ 4 ] );
  56. }  else  {
  57. request. send ( null );
  58. }
  59. }  catch  (errv )  {
  60. alert ( "The application cannot contact the server at the moment. "  +
  61. "Please try again in a few seconds.\n"  +
  62. "Error detail: " errv. message );
  63. }
  64. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值