问题
How do you emulate Form's 'POST'
action with target="_blank"
in XMLHttpRequest
? (ie post data and open target page in a new tab)
回答1:
gBrowser
offers this functionality right out of the box.
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});
来源:https://stackoverflow.com/questions/24776180/xmlhttprequest-post-and-open-target-page-in-new-window-tab