微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在端口8080上运行的Apache会阻止JavaScript中动态加载的脚本吗?

在我的个人Linode上有一个不错的PHP / HTML / JS原型,然后尝试将其放入工作机中.

页面使用一些JavaScript动态添加一个脚本标签.这是一堆基于不同时间片更新的Google图表.该代码如下所示:

// jQuery $.post to send the beginning and end timestamps
$.post("channel_functions.PHP",data_to_post,function(data){
   // the data that's returned is the javascript I want to load
   var script = document.createElement('script');
   var head= document.getElementsByTagName('head')[0];
   var script= document.createElement('script');
   var text = document.createTextNode(data);
   script.type= 'text/javascript';
   script.id = 'chart_data';
   script.appendChild(text);
   // Adding script tag to page
   head.appendChild(script);
   // Call the function I kNow were present in the script tag
   loadTheCharts();
});

function loadTheCharts() {
   // These are the functions that were loaded dynamically
   // By this point the script tag is supposed be loaded,added and eval'd
   function1();
   function2();
}

在将它们添加到dom之前,function1()和function2()不存在,但是直到$.post运行之后,我才调用loadTheCharts(),因此这似乎不是问题.

我是您母亲警告过您的那些肮脏的PHP编码人员之一,因此我对JavaScript的了解不深,超出了我在O’Reilly典型书籍中所读到的内容.但是这段代码在我的个人开发服务器上运行良好,所以我想知道为什么它在新机器上不起作用.

据我所知,设置的唯一区别是新机器在端口8080上运行,因此它是192.168.blah.blah:8080 / index.PHP而不是nicedomain.com/index.PHP.

我看到当我使用网站管理员工具“查看生成的源代码”时确实将代码添加到了dom中,但是在Firebug中,我收到了类似“ function2()undefined”的错误,即使我的理解是,当添加到 .

我的问题:考虑到我已经布置了什么,并且该机器正在:8080上运行,是否有任何人可以想到为何要在Linode而不是在Linode上定义像function2()这样的动态加载函数的原因呢? 8080上运行Apache的计算机?

最佳答案
jQuery支持javascript响应:

$.post("channel_functions.PHP",function (data,textStatus,xhr) {loadTheCharts()},'script');

但是,按照文档说明,dataType为“ script”会将跨域POST转换为GET.

eval的主要问题是eval-ed代码继承了eval所在的作用域.相反,您可以使用jQuery.globalEval.尝试如下操作:

$.post("channel_functions.PHP",xhr) {
           /* data might have errors,which will cause an exception.
              We'll let the default exception handler catch & log it.
            */
           $.globalEval(data);
           loadTheCharts();
       });

原文地址:https://www.jb51.cc/html/530652.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐