为什么PHP-生成的JavaScript中通过AJAX加载的文件不能正常工作?不能正常、加载、文件、工作

2023-09-10 20:11:03 作者:心浪微勃

我加载的.php 通过ExtJS的AJAX调用这样的文件:

I am loading a .php file via an ExtJS AJAX call like this:

menuItemApplication.header.on('click', function() {             
    Ext.Ajax.request({
        url: 'content/view_application.php',
        success: function(objServerResponse) {
            var responseText = objServerResponse.responseText;
            regionContent.update(responseText);
            var scripts, scriptsFinder=/<script[^>]*>([\s\S]+)<\/script>/gi;
            while(scripts=scriptsFinder.exec(responseText)) {
                eval(scripts[1]);
            }
        }
    });
});

JavaScript中的加载PHP文件执行罚款:

Javascript in the loaded .php file executes fine:

<script type="text/javascript">
     regionContent.update('changed region content from within application view');
</script>

但是,如果在 Javascript的正在通过生成的 PHP ,它是不执行

But if the Javascript is being generated via PHP, it is not executed:

<?php
echo "<script type=\"text/javascript\">\n";
echo "regionContent.update('changed region content from within application view')';\n";
echo "</script>\n";
?>

是的反应是一样的,在Firebug的网络面板,这里的JavaScript 看到作品

在这里,用PHP生成Javascript中,它的不能正常工作

And here with PHP-generated Javascript, which does not work:

我怎样才能获得PHP生成Javascript来执行?

推荐答案

这些反应是不一样的。检查 ';与 ;底有。

Those responses aren't the same. Check the '; versus ; at the end there.

<?php
echo "<script type=\"text/javascript\">\n";
echo "regionContent.update('changed region content from within application view')';\n";
echo "</script>\n";
?>

对战

<?php
echo "<script type=\"text/javascript\">\n";
echo "regionContent.update('changed region content from within application view');\n";
echo "</script>\n";
?>