简体   繁体   English

如何在Ajax请求中包含Javascript脚本标签?

[英]How can I include Javascript script tags in my Ajax request?

I'm using MooTools 1.4. 我正在使用MooTools 1.4。 How do I dynamically include script tags on my page? 如何在页面上动态包含脚本标签? The response from the Ajax requset below is supposed to contain tags ... 来自以下Ajax requset的响应应该包含标签...

window.addEvent('domready', function() {
    $('submit').addEvent('click', function(event) {
        var filename = $('filename').value;
        //prevent the page from changing
        event.stop();
        //make the ajax call
        var req = new Request({
            method: 'get',
            url: 'renderpage?id=' + escape(filename),
            data: { },
            evalScripts: true,
            onRequest: function() { 
                // on request
            },
            onComplete: function(response) {
                $('content').set('html',response);
                // Add the Ajax call where we save the data.
                ...
                });
            }
        }).send();
    }); 
});

But when I look at the response, they are getting stripped out. 但是,当我查看响应时,它们就被剥夺了。 I would like them included and evaluated. 我希望将它们包括在内并进行评估。 How can I do this? 我怎样才能做到这一点?

Use Request.HTML . 使用Request.HTML

    var req = new Request.HTML({
        url: 'renderpage?id=' + escape(filename),
        update: $('content'),
        onRequest: function() { 
            // on request
        }
    }).get();

You could return the entire response as JS, and eval the response; 您可以将整个响应作为JS返回,并评估响应;

Then you can set the html to some variable in the response, like 然后,您可以将html设置为响应中的某个变量,例如

var new_html='(html)';

Then you can do this 那你可以做

$('content').set('html',new_html);

The parameters passed to onComplete are: 传递给onComplete的参数为:

nodeTree, xhr, responseHTML, responseScripts

So: 所以:

new Request.HTML({
  // ...
  onComplete : function(nodeTree, xhr, responseHTML, responseScripts) {
    eval(responseScripts);
  }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我是否应该在我的SCRIPT标签中包含type =“ text / javascript”? - Should I include type=“text/javascript” in my SCRIPT tags? 如何使JavaScript代码执行等到加载并执行带脚本的AJAX请求? - How can I get make JavaScript code execution to wait until an AJAX request with script is loaded and executed? jQuery / Javascript:AJAX请求引入的HTML不包括<script> elements - jQuery/Javascript: HTML pulled in by AJAX request does not include <script> elements 我的Javascript Ajax请求可在Phonegap index.html中使用,但不能在其他任何页面中使用,如何解决此问题? - My Javascript Ajax request works in Phonegap index.html but not in any other pages, how can I fix this? 如何在Visualforce页面上的CSS中包含JAVASCRIPT - How can I include JAVASCRIPT in my CSS on my visualforce page 如何合并AJAX脚本和TOGGLE脚本? - How can i combine my AJAX script and TOGGLE script? 如何使用JavaScript向文件系统发出AJAX请求? - How do I make an AJAX request to my file system in JavaScript? 我该如何添加 <td> 用JavaScript标记我的html表格? - How can I add <td> tags to my html table with JavaScript? 我向Twitter API提出了请求。 如何从PHP脚本获得对JavaScript的JSON响应? - I made a request to the Twitter API. How can I get the JSON response from my PHP script to my JavaScript? 如何将我的 JavaScript 文件包含到 HTML 页面中? - How can I include my JavaScript file into an HTML page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM