惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
宝玉的分享
宝玉的分享
G
Google Developers Blog
T
Tailwind CSS Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
V
Visual Studio Blog
博客园 - Franky
S
SegmentFault 最新的问题
Jina AI
Jina AI
爱范儿
爱范儿
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
D
DataBreaches.Net
C
Check Point Blog
月光博客
月光博客
P
Proofpoint News Feed
T
The Blog of Author Tim Ferriss
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
MongoDB | Blog
MongoDB | Blog
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Martin Fowler
Martin Fowler

Allen Hua 的网络博客

由于 Linux 桌面没有一个好用的天气程序就写了一个跨平台的CheckitoutWeather 给 caesium-image-compressor 图片压缩程序构建了 Linux AppImage v2.8.5 最新版 debian13(debian trixie)安装了nvidia闭源驱动后从x11切换到wayland的方法 山间摩旅追风,偶遇一场绚烂晚霞 最近使用debian系统的一些心得 机械革命无界14Pro笔记本debian forky成功驱动内置扬声器和麦克风 记录 typecho 1.2.0 升级到 1.3.0 过程 开发了一款openwrt插件:文本剪贴板 解决机械革命笔记本内屏高刷240Hz闪屏问题 使用 Java 写了一个局域网端口扫描器 - Allen Hua 的网络博客 openwrt使用外置根extroot机制扩展根分区大小 - Allen Hua 的网络博客 将机场ss节点批量转换成ss字符串链接批量添加到passwall - Allen Hua 的网络博客 给机械革命钛钽plus换屏:NY2换成NZ2 - Allen Hua 的网络博客 记录一次pve宿主机和上面的debian虚机无故down机事件 - Allen Hua 的网络博客 给图床部署cdn腾讯云的edgeone并排查Cache-Control max-age 3600的问题 - Allen Hua 的网络博客 从高山草甸到徽派古村:武功山反穿与皖浙赣自驾行记 - Allen Hua 的网络博客 增程器就是充电宝?别被忽悠了 - Allen Hua 的网络博客 浦口龙虎巷扫街,记录人间真实 博客图片压缩方案更新|AVIF|WebP|MozJPEG|标准JPEG Windows 电脑使用 Obs Studio 录制各个网站视频/桌面画面教程 为typecho博客添加latex支持 新能源汽车之纯电车使用交流慢充和直流快充的充电损耗对比 2025年3月更新全国5A景区名录 2025最新查看小米/红米手机电池健康度和循环次数方法 完美解决 seafile FILE_SERVER_ROOT 配置导致的内网外网不能同时访问和上传下载的问题 纯css实现typecho博客文章文字spoiler剧透效果 我对Typecho Facile主题的一些修改,图片懒加载优化,样式定制 2024年8月我的宜昌 - 重庆 - 川西小环线自驾旅行分享 2022年打卡南京市区人防工程纳凉点 2024年带着A7C2+腾龙28-200 再次来到红山森林动物园
记一次 OpenWrt luci 显示正在“收集数据”,使用公网 IP nginx...
Allen Hua · 2022-07-18 · via Allen Hua 的网络博客
阅读量:6024

warning: 这篇文章距离上次修改已过1403天,其中的内容可能已经有所变动。

搜遍了全网,只有这个链接 ( https://forum.openwrt.org/t/solved-luci-overview-page-has-blank-fields/16967 ) 类似。但我使用 frp 映射后的域名访问主页又能正常展示这些字段。看了下接口返回均是 200,response 也都正常。

我的是下面这样的

通过 F12 Console 可以看到 Uncaught TypeError: Cannot read properties of null (reading 'wan') 这样的报错,实际上 index 文件 /usr/lib/lua/luci/view/admin_status/index.htm info 会取 /cgi-bin/luci/ 接口返回的对象中各个成员,wan 只是他第一个获取的。debug 发现 info 是 null。所以问题肯定不能通过 var xxx = info && info.wan 这样去解决,后面还有一大堆 key 呢,实际上我也这么做过,问题并不能得到解决。

我通过 frp 基于公网 IP 服务器映射出来的域名可以正常加载这些信息,接口都有返回,但是我家里宽带现在具备了公网 IP,我就直接通过一个域名加非常规端口然后基于 https (在 OpenWrt 上跑了一个 nginx)访问就始终存在 info 是 null 的问题。

经过一番尝试,通过如下的方法解决了。

找到文件 /www/luci-static/resources/xhr.js,找到其中的 xhr.onreadystatechange = function () {

解析 xhr 的 responseText 是通过 json = eval('(' + xhr.responseText + ')'); 经过测试发现始终是 null,这一句里的单引号换成双引号也不行。

最终更改 json 解析的方式,这一行改成了 json = JSON.parse(xhr.responseText);

再次尝试,有了惊喜,发现日志打印出了 json object!但是遇到了新的问题

Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "default-src 'self' http: https: data: blob: 'unsafe-inline'".

原本的 xhr.js

xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                var json = null;
                if (xhr.getResponseHeader("Content-Type") == "application/json") {
                    try {
                        json = eval('(' + xhr.responseText + ')');
                    }
                    catch (e) {
                        json = null;
                    }
                }

                callback(xhr, json);
            }
        }

更改之后的 xhr.js

xhr.onreadystatechange = function()
                {
                        if (xhr.readyState == 4) {
                                var json = null;
                                if (xhr.getResponseHeader("Content-Type") == "application/json") {
                                        console.log('I am the original xhr, ', xhr);
                                        try {
                                                //json = eval('(' + xhr.responseText + ')');
                                                json = JSON.parse(xhr.responseText);
                                        }
                                        catch(e) {
                                                console.debug('parsing json failed: ', e);
                                                json = null;
                                        }
                                        console.log('I am the result json: ', json);
                                }

                                callback(xhr, json);
                        }
                }

console.log 建议删除,console.debug 可以保留。

经过一番学习、搜索,发现了此帖 Getting error Uncaught EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script

在 nginx 配置 location / { 下增加一行

add_header Content-Security-Policy "default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval'" always;

热更新一下 nginx,再次访问,成功了!

最后总结一下 json string to json object in JavaScript

# convert a json string to a json object

1. var json = JSON.parse(str);
2. var json = eval("(" + str + ")");
3. var json = (new Function("return " + str))();

不过需要注意的是,JSON.parse(str) 需要 json string 里 key value 均使用双引号包裹,整体使用单引号包裹。而 eval 和 new Function 的形式是单引号双引号均可,但是还需要注意,如果 key value 使用了单引号,那么外部需要使用双引号包裹,否则 js 报错。