node.js Https请求传递数据

我的代码如下:

参考了网上大多说的是req.write(contents);我试了这样写没有效果

而是在url拼接?contents 来传递数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import * as https from "https";
var querystring = require('querystring');

//传递到后台的数据
var data = "1";
var post_data = { classId: data };
var contents = querystring.stringify(post_data);

var options = {
hostname: "www.baidu.com",
port: 443,
path: "/bfi-web/api/student/course/list_new?" + contents,
method: "POST",
rejectUnauthorized: false,
headers: {
Accept: "*/*",
"Accept-Encoding": "utf-8",
"Accept-Language": "zh-CN,zh;q=0.8",
Connection: "keep-alive",
Host: "www.eluzhu.com",
token: token,
version: "2.6",
from: "IDE",
},
};

var mData = '';
var req = https.request(options, function (res) {
res.setEncoding('utf-8');
res.on("data", function (d) {
console.log('===================================data===========' + d);
mData += d;
});
res.on("end", function () {
var data = JSON.parse(mData);
// console.log('===================================data===========' + mData);
if (data.code == "000000") {
MyTreeProvider.datas = data.data.list;
MyTreeProvider.initMyTreeList();
}
else {
vscode.window.showInformationMessage("请求失败:" + data.msg);
}
});
});
// req.write(contents);
req.on("error", function (e) {
console.error("====================================================" + e);
});
req.end();

本文参考来自:https://www.imooc.com/qadetail/181878

文章目录
  1. 1. 我的代码如下:
  2. 2. 参考了网上大多说的是req.write(contents);我试了这样写没有效果
  3. 3. 而是在url拼接?contents 来传递数据