[英]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.