繁体   English   中英

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

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

我正在使用MooTools 1.4。 如何在页面上动态包含脚本标签? 来自以下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();
    }); 
});

但是,当我查看响应时,它们就被剥夺了。 我希望将它们包括在内并进行评估。 我怎样才能做到这一点?

使用Request.HTML

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

您可以将整个响应作为JS返回,并评估响应;

然后,您可以将html设置为响应中的某个变量,例如

var new_html='(html)';

那你可以做

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

传递给onComplete的参数为:

nodeTree, xhr, responseHTML, responseScripts

所以:

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM