【问题标题】:How to (ajax) post and execute response?如何(ajax)发布和执行响应?
【发布时间】:2013-01-17 16:18:01
【问题描述】:

我们有以下情况:

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>

请帮忙。谢谢。

【问题讨论】:

  • 什么是选项 1 和选项 2。如果您尝试打开一个需要整体呈现的新 html 页面。您不应该在该页面上写入数据。事实上,您应该只将 html 链接传递给 win.document,并且应该只在新窗口中打开链接,一切都会正常执行。从一个页面到另一个页面的动态 url 生成。

标签: javascript jquery asp.net post httphandler


【解决方案1】:

在你的 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(检查它是否不为空或为空,甚至是有效的 url),然后使用该 url 打开一个新窗口。阅读 W3C window.open 获取参数
  4. 如果您出于某种原因想要打开和关闭它,只需保留窗口句柄即可,但您可以在该新窗口的 dom 就绪事件中执行此操作,否则您可能会在其 dom 完全加载之前关闭它。 (其他人可能有更好的方法)
  5. 如果它不是 url 类型,那么你在这个页面上做你通常的事情。

如果这没有意义,让我们讨论。

【讨论】:

  • 对不起,我不明白你的代码。 // 当新窗口的所有 html 由浏览器呈现时,无需将数据写入新窗口。 -> 这部分是必不可少的,因为它包含带有 Jquery 脚本等的 html 代码...
  • 如果您的函数像页面一样返回 html,那么只需在新窗口中调用 url。您不需要获取所有 html,然后通过此功能将其注入新窗口。 ajax 应该只返回 url,当你将此 url 传递给 window.open 时,它将在新窗口中打开它。
  • 回调可以返回动态html,无法预测。在这种情况下,我们不知道 url,所以我们不能只在新窗口中打开。
  • 什么是动态html?回调意味着您请求服务器,它会将 html 发送回来。您只需要在第一个请求中传递参数即可生成动态 url 以获取这些参数和生成的 html。
  • 请查看我的第一篇文章。对于动态 html,我的意思是底部示例:“案例 2 中的响应......”。我们不能影响来自 HttpHandler 的响应。答案是带有 JavaScript 和 FORM 的 HTML 代码,这也在下一页上发帖。简而言之,必须执行 html。不过,感谢您提出建设性的想法。
猜你喜欢
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多