首页 > 解决方案 > 调用函数构造函数返回......什么都没有。为什么?

问题描述

我有一个脚本文件,它使用函数(类似于)生成网页:

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 没问题。

在此处输入图像描述 在此处输入图像描述

标签: javascriptxmlhttprequest

解决方案


推荐阅读