【问题标题】:XMLHttpRequest POST and open target page in new window/tabXMLHttpRequest POST 并在新窗口/选项卡中打开目标页面
【发布时间】:2014-07-16 08:45:11
【问题描述】:

如何在XMLHttpRequest 中使用target="_blank" 模拟Form 的'POST' 操作? (即发布数据并在新标签中打开目标页面)

【问题讨论】:

    标签: javascript firefox-addon firefox-addon-restartless


    【解决方案1】:

    gBrowser 开箱即用地提供此功能。

    var dataStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
    dataStream.data = "foo=bar&alpha=beta"; // make sure the values are properly encoded with encodeURIComponent
    
    var postStream = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
    postStream.addHeader("Content-Type", "application/x-www-form-urlencoded");
    postStream.addContentLength = true;
    postStream.setData(dataStream);
    
    gBrowser.loadOneTab("http://www.example.com/", {inBackground: false, postData: postStream});
    

    【讨论】:

    • 太棒了 :) .... 我要试试。关于dataStream.data,我怎样才能传递一个本地文件,即'file:///C:/.../root.png'
    • 非常有用的东西,谢谢。用这种方法提交文件也不错! values 被正确编码的意思是:dataStream.data = encodeURIComponent("foo") + "=" + encodeURIComponent("ba-%r") + "&" + encodeURIComponent("@find") + "=" + encodeURIComponent("ba+$@r"); 对吗?
    • @erosman 尝试将 use FormData 作为 postStream 传递,但我不知道这是否适用于 loadOneTab
    猜你喜欢
    • 1970-01-01
    • 2015-07-26
    • 2015-03-10
    • 1970-01-01
    • 2014-02-02
    • 2014-10-11
    • 2012-04-30
    • 2017-01-05
    • 1970-01-01
    相关资源
    最近更新 更多