▒ 目录 ▒
🛫 导读
需求
开发环境
版本号 | 描述 | |
---|---|---|
文章日期 | 2023-11-07 | |
操作系统 | Win10 - 22H2 | 19045.3570 |
示例工作目录 | J:\_ALL\JOB\sw\_nginx\map-ys | 执行json-server服务器 |
1️⃣ Adblock等插件拦截
2️⃣ 【失败】Content-Security-Policy
启动服务器json-server
html中的meta字段
3️⃣ 【失败】https vs http
webPreferences & allowRunningInsecureContent
new BrowserWindow({
webPreferences:{//网页功能的设置
// nodeIntegration: true,//是否集成node
// devTools:false,//是否开启 DevTools
webSecurity: false, //是否禁用同源策略(上线时删除此配置)
allowRunningInsecureContent: true,
// ...
}
}
disable-features
//解决10.X版本跨域不成功问题(上线删除)
app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors');
4️⃣ 【失败】检测fetch
fetch被魔改了
5️⃣ 【失败】使用axios
插入axios库
6️⃣ 【成功】require(‘http’)
var http = require('http');
function post(action,send,callback){
var options = {
hostname: '127.0.0.1',
port: 16010,
path: action,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'/* ,
'Content-Length': send.length */
}
};
var req = http.request(options, function (res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
// 定义了一个post变量,用于暂存请求体的信息
var body="";
res.setEncoding('utf8');
// 通过res的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
res.on('data', function (chunk) {
// console.log('BODY: ' + chunk);
body += chunk;
});
// 在res的end事件触发后,通过JSON.parse将post解析为真正的POST请求格式,然后调用传递过来的回调函数处理数据
res.on('end', function(){
// console.log("body = "+body);
var json = JSON.parse(body);
callback(json);
});
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.write(send);
req.end();
}
post('/posts', '{"id":3}', ()=>{})
7️⃣ 【完美解决】取消webRequest.onBeforeRequest
function blockHttpRequests() {
// 注释掉
return;
var e;
i()
.session.fromPartition(
(e = g.windowOptions.webPreferences.partition) !== null &&
e !== void 0
? e
: ""
)
.webRequest.onBeforeRequest({ urls: ["http://*/*"] }, (e, t) => {
t({
cancel:
e.webContents && e.webContents.getURL().startsWith("file://"),
});
});
}