繁体   English   中英

如何(ajax)发布和执行响应?

[英]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);});
}

总体逻辑是这样

  1. 您在doPost上进行ajax调用
  2. 找出返回的数据是url类型还是需要在新窗口中打开的任何内容
  3. 如果是url类型,它将具有一个url(检查它是否不是null或为空,甚至不是有效的url),然后使用该url打开一个新窗口。 阅读W3C window.open以获取参数
  4. 如果由于某种原因要打开和关闭它,只需保持窗口句柄即可,但是您可以在新窗口的domdom事件中执行此操作,否则可能会在完全加载dom之前关闭它。 (其他人可能有更好的方法)
  5. 如果不是url类型,则在此页面上执行常规操作。

如果这没有意义,请讨论。

暂无
暂无

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

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