http-post.js 917 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. var http = require('http');
  2. var querystring = require('querystring');
  3. function httpPost(path,data){
  4. var post_data = JSON.stringify(data);
  5. var options = {
  6. host: 'xcx.chuangzhikeji.com',
  7. port: 80,
  8. path: path,
  9. method: 'POST',
  10. headers:{
  11. 'Content-Type':'application/json'
  12. }
  13. };
  14. var req = http.request(options, function (res) {
  15. if(res.statusCode === 200){
  16. console.log('发送后台数据成功')
  17. }else{
  18. console.log('STATUS: ' + res.statusCode);
  19. console.log('HEADERS: ' + JSON.stringify(res.headers));
  20. res.setEncoding('utf8');
  21. res.on('data', function (chunk) {
  22. console.log('BODY: ' + chunk);
  23. });
  24. }
  25. });
  26. // write data to request body
  27. req.write(post_data);
  28. req.end();
  29. }
  30. exports.httpPost = httpPost;