代码之家  ›  专栏  ›  技术社区  ›  Peter

Jquery post,在新窗口中响应

  •  39
  • Peter  · 技术社区  · 15 年前

    我有个剧本打开文档准备将数据发布到另一页。该页面以封装在一个div标记中的一些HTML作为响应。

    有什么线索吗?

    以下是我根据米勒博士的建议创建的片段。

     <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
     <script type="text/javascript">
    $(document).ready(function() {
    var packslip_id = 35592;
    var po_no = 0018439;
    var  box_no = 1;
        $.post("https://example.com/barcode/generate", { packing_slip: packslip_id, reference: po_no, total_boxes: box_no}, 
        function (data) {
            alert(data);
            var win=window.open('about:blank');
            with(win.document)
            {
                open();
                write(data);
                close();
            }
        });
     });
    

    4 回复  |  直到 13 年前
        1
  •  68
  •   Dr.Molle    8 年前

    使用弹出窗口文档的write()-方法将标记放在那里:

    $.post(url, function (data) {
        var w = window.open('about:blank');
        w.document.open();
        w.document.write(data);
        w.document.close();
    });
    
        2
  •  27
  •   Rocklan    12 年前

    Accepted answer不适用于“use strict”,因为“with”语句会抛出错误。因此:

    $.post(url, function (data) {
        var w = window.open('about:blank', 'windowname');
        w.document.write(data);
        w.document.close();
    });
    

    另外,请确保“windowname”中没有任何空格,因为这将在IE中失败:)

        3
  •  6
  •   Dr.Molle    15 年前

    如果您不需要有关请求数据的反馈,也不需要在打开程序和弹出窗口之间进行任何交互,则可以在弹出窗口中发布隐藏表单:

    例子:

    <form method="post" target="popup" id="formID" style="display:none" action="https://example.com/barcode/generate" >
      <input type="hidden" name="packing_slip" value="35592" />
      <input type="hidden" name="reference" value="0018439" />
      <input type="hidden" name="total_boxes" value="1" />
    </form>
    <script type="text/javascript">
    window.open('about:blank','popup','width=300,height=200')
    document.getElementById('formID').submit();
    </script>
    

        4
  •  1
  •   NickNuke    7 年前

    我用一个ajax帖子完成了它,然后用一个数据url返回:

    $(document).ready(function () {
        var exportClick = function () {
            $.ajax({
               url: "/api/test.php",
               type: "POST",
               dataType: "text",
               data: {
                  action: "getCSV",
                  filter: "name = 'smith'",
               },
               success: function(data) {
                  var w = window.open('data:text/csv;charset=utf-8,' + encodeURIComponent(data));
                  w.focus();
               },
               error: function () {
                  alert('Problem getting data');
               },
            });
        }
    });