[英]How to (ajax) post and execute response?
我们有以下情况:
在default.aspx
我们有一个链接:
<a href="javascript:doPost()">test</a>.
和JS代码:
function doPost() {
$.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
if(data.indexOf("http://")==0)
window.open(data);
else{
var win=window.open();
with(win.document) {
open();
write(data); //-> how to execute this HTML code? The code also includes references to other js files.
close();
}
}
}).error(function(msg){document.write(msg.responseText);});
}
回调可以首先是 必须执行 的URL地址或第二个html代码 。 选项1适合,但在选项2中,将打开一个新窗口,其中已编写但未执行代码。
很明显,由于它发生在流中,因此无法执行。 那么问题是,如何解决呢? 也许是refresh()
或类似的东西?
由于客户的需求,无法更改工作流程,因此必须在doPost()
解决。
编辑
情况2的响应是这样的HTML。 这部分应该执行 :
<HTML><HEAD>
<SCRIPT type=text/javascript src="http://code.jquery.com/jquery-latest.js">
</SCRIPT>
<SCRIPT type=text/javascript>
$(document).ready(function() {
do_something...
});
</SCRIPT>
</HEAD>
<BODY>
<FORM>...</FORM>
</BODY>
</HTML>
请帮忙。 谢谢。
在您的JS代码中,应该是这样的:
function doPost() {
$.post('AnHttpHandlerPage.aspx',"{some_data:...}", function(data) {
//if(data.indexOf("http://")==0)
if (data.type!="url") //i will add a data type to my returned json so i can differentiate if its url or html to show on page.
window.open(); // I dont know why this is there. You should
else{
var win=window.open(data.url); //This data.url should spit out the whole page you want in new window. If its external it would be fine. if its internal maybe you can have an Action on one of your controllers that spit it with head body js css etc.
/* with(win.document) {
open();
write(data); //-> how to execute this HTML code? The code also includes references to other js files.
close(); */ // No need to write data to new window when its all html to be rendered by browser. Why is this a requirement.
}
}
}).error(function(msg){document.write(msg.responseText);});
}
总体逻辑是这样
如果这没有意义,请讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.