Electron接口请求

前言

Electron中可以使用两种请求方式

  • 使用NodeJS的库进行接口请求
  • 使用前端库进行接口请求

注意

使用前端发起请求才能在DevTools中查看到。

NodeJS接口请求

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 request from "request";

export function mpost(url, para) {
return new Promise(function(resolve, reject) {
request(
{
url: url,
method: "POST",
json: true,
headers: {
"content-type": "application/json"
},
body: para
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
if (body.code === 0) {
resolve(body["obj"]);
} else {
reject(body.msg);
}
} else {
reject("请求接口失败");
}
}
);
});
}

export function mget(url) {
return new Promise(function(resolve, reject) {
request(
{
url: url,
method: "GET",
json: true,
headers: {
"content-type": "application/json"
}
},
function(error, response, body) {
if (!error && response.statusCode === 200) {
resolve(body);
} else {
reject("请求接口失败");
}
}
);
});
}

前端库Axios

https://www.psvmc.cn/article/2019-01-08-vue-axios-upload.html