XMLHttpRequest POST和在新窗口/标签页中打开目标页面

3

如何使用XMLHttpRequest模拟带有target="_blank"属性的表单'POST'操作?(即提交数据并在新标签页中打开目标页面)

1个回答

2
可以直接提供这个功能。
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' - erosman
非常有用的东西,谢谢!使用这种方法提交文件也不错!你所说的“正确编码值”是指像这样编码:"dataStream.data = encodeURIComponent("foo") + "=" + encodeURIComponent("ba-%r") + "&" + encodeURIComponent("@find") + "=" + encodeURIComponent("ba+$@r");" 对吗? - Noitidart
1
@erosman 尝试使用 FormData 并将其作为 postStream 传递,虽然我不知道是否与 loadOneTab 兼容。 - paa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接