Posted By

jerryvig on 06/25/13


Tagged

http ajax node jquery nodejs


Versions (?)

HTTP Requests from Node.js with jQuery


 / Published in: JavaScript
 

These are examples of how to do HTTP GET, POST, PUT, and DELETE from Node.js using jQuery.

  1. /**
  2.  * Be sure to install jQuery as a node module first.
  3.  * > npm install jquery
  4.  *
  5.  */
  6. var $ = require('jquery');
  7.  
  8. var BASE_URL = 'http://localhost:8080/mock-server-manager';
  9.  
  10. var requestObject = {
  11. macAddress: 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ',
  12. boxName: 'BOX_Z',
  13. fileName: 'empty-list.xml'
  14. };
  15.  
  16. var updateRequestObject = {
  17. macAddress: 'ZZ:ZZ:ZZ:ZZ:ZZ:ZZ',
  18. boxName: 'ZZ TOP LA GRANGE',
  19. fileName: 'empty-list.xml'
  20. };
  21.  
  22. /**
  23.  * An example HTTP GET request.
  24.  */
  25. var listBoxes = function(){
  26. $.get(BASE_URL + '/rest/manager/listBoxes', '', function(data){
  27. var prettyObject = JSON.parse(data);
  28. console.log(prettyObject);
  29. }, 'text');
  30. };
  31.  
  32. /**
  33.  * An example HTTP POST request.
  34.  */
  35. var addStb = function(){
  36. $.ajax({'url': BASE_URL + '/rest/manager/addStb',
  37. 'type' : 'POST',
  38. 'headers' : {'Content-Type' : 'application/json'},
  39. 'data' : JSON.stringify(requestObject),
  40. 'processData' : false,
  41. 'success' : function(data){
  42. console.log(data);
  43. },
  44. 'error': function(jqXHR, data){
  45. console.log(data);
  46. //comcast.cvs.apps.alerts.test.showErrorDialog( '<div style="color:red;font-weight:bold;">' +
  47. // 'Failed to save the settop box. See server logs for problem.</div>' );
  48. },
  49. 'dataType' : 'text'
  50. });
  51. };
  52.  
  53. /**
  54.  * An example HTTP PUT request.
  55.  */
  56. var updateStb = function(){
  57. $.ajax({'url': BASE_URL + '/rest/manager/updateStb/'+updateRequestObject.macAddress,
  58. 'type' : 'PUT',
  59. 'headers' : {'Content-Type' : 'application/json'},
  60. 'data' : JSON.stringify(updateRequestObject),
  61. 'processData' : false,
  62. 'success' : function(data){
  63. console.log( data );
  64. },
  65. 'error': function(jqXHR, data){
  66. console.log( data );
  67.  
  68. console.log( '<div style="color:red;font-weight:bold;">' +
  69. 'Failed to DELETE the settop box. See server logs for problem.</div>');
  70. },
  71. 'dataType' : 'text'
  72. });
  73. };
  74.  
  75. /**
  76.  * Another example of HTTP GET request.
  77.  */
  78. var showStb = function(){
  79. var url = BASE_URL + '/rest/manager/showBox/' + requestObject.macAddress;
  80. $.get(url, '', function(data){
  81. var prettyObject = JSON.parse(data);
  82. console.log(prettyObject);
  83. }, 'text');
  84. };
  85.  
  86. /**
  87.  * An example HTTP DELETE request.
  88.  */
  89. var deleteStb = function(){
  90. $.ajax({'url': BASE_URL + '/rest/manager/deleteStb/'+requestObject.macAddress,
  91. 'type' : 'DELETE',
  92. 'success' : function(data){
  93. console.log( data );
  94. },
  95. 'error': function(jqXHR, data){
  96. console.log( data );
  97.  
  98. console.log( '<div style="color:red;font-weight:bold;">' +
  99. 'Failed to DELETE the settop box. See server logs for problem.</div>');
  100. },
  101. 'dataType' : 'text'
  102. });
  103. };
  104.  
  105. //addStb();
  106. //updateStb();
  107. //deleteStb();
  108. showStb();
  109. //listBoxes();

Report this snippet  

You need to login to post a comment.