1. request方法:

提交评论到慕课网:

 1 var http = require('http');
 2 var querystring = require('querystring');
 3 
 4 var postData = querystring.stringify({
 5     'content': '很喜欢Scot老师的课程. 希望尽快学会nodejs!',
 6     'cid': 348
 7 });
 8 
 9 var options = {
10     hostname: 'www.imooc.com',
11     port: 80,
12     path: '/course/docomment',
13     method: 'POST',
14     headers: {
15         'Accept':'application/json, text/javascript, */*; q=0.01',
16         'Accept-Encoding':'gzip, deflate',
17         'Accept-Language':'zh-CN,zh;q=0.8',
18         'Connection':'keep-alive',
19         'Content-Length':postData.length,
20         'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
21         'Cookie':'PHPSESSID=lvk77ha5lgp411jqbl2n9dovd0; imooc_uuid=94406a08-7461-473e-8676-a730e6d1f16a; imooc_isnew=1; imooc_isnew_ct=1501826723; loginstate=1; apsid=E0M2ExNjBmNWQwN2Q0MDc0YzhhZWUwMmY4MTg3NmEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMTM1NTg1NAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB4X2RhY2hlbmdAMTI2LmNvbQAAAAAAAAAAAAAAAAAAAGY3YzYwNzdjYjlmMjBkYTA0OWI0OWE3ZjhiZDgxMzEz6Q6EWekOhFk%3DNm; last_login_username=x_dacheng%40126.com; Hm_lvt_f0cfcccd7b1393990c78efdeebff3968=1501826670; Hm_lpvt_f0cfcccd7b1393990c78efdeebff3968=1501826738; IMCDNS=0; cvde=59840ea3a723c-7',
22         'Host':'www.imooc.com',
23         'Origin':'http://www.imooc.com',
24         'Referer':'http://www.imooc.com/comment/348',
25         'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36',
26         'X-Requested-With':'XMLHttpRequest'
27     }
28 };
29 
30 
31 //发送请求:
32 var req = http.request(options, function(res){
33 
34     console.log('status: ' + res.statusCode);
35     console.log('headers: ' + JSON.stringify(res.headers));
36 
37     res.on('data', function(chunk){
38         console.log(Buffer.isBuffer(chunk));
39         console.log(typeof chunk);
40     });
41 
42     res.on('end', function(){
43         console.log('评论完毕!');
44     });
45 });
46 
47 
48 req.on('error', function(e){
49     console.log('Error: ' + e.message);
50 });
51 
52 //写入请求数据:
53 req.write(postData);
54 
55 //结束请求:必须写的:
56 req.end();

 

 

 

注意:

  第56行的代码:   req.end(); 必须写.

 

------------------

 

2. get() 方法.

get方法是 对 request方法的封装,    get方法 自带 req.end()

 

 

 nodejs 请求端: get.js

 1 var http = require('http');
 2 var querystring = require('querystring');
 3 
 4 
 5 var postData = {
 6     'name': '小明',
 7     'age': 26
 8 };
 9 
10 var postDataStr = querystring.stringify(postData);
11 
12 
13 http.get('http://www.a.com/response.php?'+postDataStr, function(res){
14     console.log('status: ' + res.statusCode);
15     console.log('headers: ' + JSON.stringify(res.headers));
16     var txt = '';
17     res.on('data', function(chunk){
18         // console.log(Buffer.isBuffer(chunk));
19         // console.log(typeof chunk);
20 
21 
22         txt += chunk;
23     });
24 
25     res.on('end', function(){
26         console.log(typeof txt);
27         console.log(txt);
28 
29         console.log(typeof JSON.parse(txt));
30         console.log(JSON.parse(txt));
31         console.log(JSON.parse(txt).name);
32         console.log(querystring.unescape(JSON.parse(txt).name));
33     });
34 }).on('error', function(e){
35     console.log('Error: '+e.message);
36 });

 

 

 PHP接收端: response.php

 

 1 <?php
 2 
 3 
 4     $name = $_GET["name"] . '--返回数据';
 5     $age = $_GET['age'] + 100;
 6 
 7     echo json_encode(array(
 8             'name' => $name,
 9             'age' => $age,
10             'address' => 'beijing'
11         ));
12 
13 ?>

 

 运行:

 

 

 注意:

  •   返回的数据 是 json格式的字符串 ,因此要使用 JSON.parse() 转化为json对象.

  

 

 

参考链接: