返回302,请教下哪里出问题了?
var http = require('http');
var querystring = require('querystring');
var postData = querystring.stringify({
'content': '老师下次更新什么时候啊?',
'cid': 348,
});
var options = {
hostname: 'www.imooc.com',
port: 80,
path: '/course/docoment',
method: 'POST',
headers: {
'Accept':'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding':'gzip, deflate',
'Accept-Language':'zh-CN,zh;q=0.8',
'Cache-Control':'no-cache',
'Connection':'keep-alive',
'Content-Length':postData.length,
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie':'//Cookie数据就不贴出来了,是直接复制粘贴过来的',
'Host':'www.imooc.com',
'Origin':'http://www.imooc.com',
'Pragma':'no-cache',
'Referer':'http://www.imooc.com/comment/348',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
};
var req = http.request(options, function(res) {
console.log('Status: ' + res.statusCode);
console.log('headers: ' + JSON.stringify(res.headers));
//为data事件注册回调函数来接受数据
res.on('data', function(chunk) {
console.log(Buffer.isBuffer(chunk));
console.log(typeof chunk);
});
//当数据接受完毕,网络连接关闭,触发end事件
res.on('end', function() {
console.log('评论完毕!');
});
});
//当有错误发生时候
req.on('error', function(e) {
console.log("error! " + e.message);
});
//把要提交的数据写入请求体
req.write(postData);
//完成请求
req.end();代码如上,node comment.js返回:
Status: 302
headers: {"server":"nginx","date":"Tue, 08 Mar 2016 06:51:41 GMT","content-type":"text/html","transfer-encoding":"chunked","connection":"keep-alive","expires":"Thu, 19 Nov 1981 08:52:00 GMT","cache-control":"no-store, no-cache, must-revalidate, post-check=0, pre-check=0","pragma":"no-cache","location":"/error/noexists"}
评论完毕!
看了好多遍代码,不知道哪里出了问题,,求教一下
