javascript - 调用函数构造函数返回......什么都没有。为什么?
问题描述
我有一个脚本文件,它使用函数(类似于)生成网页:
var get_parameters = window.location.href.split('?')[1];
var json_from_get = JSON.parse(get_parameters);
var main_function = (function(json_from_get) {
... lots of code here
})(json_from_get);
我从内联脚本标记中的 XHR 请求调用此文件,而不是直接调用:
<script>
var xhr = new XMLHttpRequest();
xhr.open('get', 'path/to/my/script.js', 1);
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
return new Function(this.response); // -> Not working!
}
};
xhr.send('parameter_1={"is-json":true,"can-you-believe":false}');
</script>
new Function()
不起作用(我什至没有收到错误 - 我没有得到任何东西),我不知道为什么。自从我到处读到 EVAL IS EVIL,我就没有尝试过使用eval
,我不想支持撒旦。所以请帮助我了解如何new Function
正确使用。
谢谢
更新中:readyState 和 status 没问题。
解决方案
推荐阅读
- docker - 错误 Docker 两次启动同一个容器
- sql - 将 SQL 查询转换为图形模型
- c# - 由于拥有实体,EF Core 2.1 将不需要的列添加到架构
- java - Android-Listview setOnItemClickListener 错误
- python - Flasks - render_template 不渲染 html 页面
- c# - 扩展方法必须在非泛型静态类中定义错误 public static
- assembly - X86:如何将xmm0的下半部分设置为0,而不影响上半部分?
- python - Redisearch 前缀搜索始终返回最大 200 的多字段索引总数
- c - CS50 PSET4 拼写器无法释放内存
- c# - 在 C# 中保持状态标志(32 项以下)的最有效方法