XMLHttpRequest POST并在新窗口/选项卡中打开目标页面

时间:2014-07-16 08:45:11

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

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

1 个答案:

答案 0 :(得分:2)

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});