在端口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();
       });

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些