0

由于 ,loadOneTab()不适用于formData,

  1. 如何formData发布到新标签?
  2. 上面新建的tab前/后台状态怎么设置?

只是Using FormData Objects中的一个示例:

var formData = new FormData();

formData.append("username", "Groucho");
formData.append("accountnum", 123456); // number 123456 is immediately converted to string "123456"

// HTML file input user's choice...
formData.append("userfile", fileInputElement.files[0]);

// JavaScript file-like object...
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});

formData.append("webmasterfile", blob);

var request = new XMLHttpRequest();
request.open("POST", "http://foo.com/submitform.php");
request.send(formData);

澄清:
带有 的普通 HTML 表单target="_blank"会将表单数据发布到新选项卡。
同样,如前所述,loadOneTab()也可以将数据 POST 到新选项卡。
是否可以使用 XMLHttpRequest 做到这一点?

4

2 回答 2

1

This is what I mean by loading responseText of xhr into a tab, can copy paste to scratchpad.

var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        switch (xhr.readyState) {
                case 4:
                   prompt('done', xhr.responseText);
                    gBrowser.loadOneTab('data:text/html, ' + encodeURIComponent(xhr.responseText), {
                      inBackground: false
                    });
            break;
          default:
        }
    };
    xhr.open("GET", "https://www.bing.com/");
    xhr.send();
于 2014-07-30T17:40:28.813 回答
1

XHR 与标签完全无关。如果你真的想 XHR 它,那么你应该使用它返回的源和更新目标选项卡的文档。

否则我只会使用loadOneTab:我会想像这样的东西变成nsIFile:

此处导入encodeFormData函数表单:https ://stackoverflow.com/a/25020668/3791822

// HTML file input user's choice...
var userfileNSIFILE = new FileUtils.File(fileInputElement.files[0].path);

// JavaScript file-like object...
var content = '<a id="a"><b id="b">hey!</b></a>'; // the body of the new file...
var blob = new Blob([content], { type: "text/xml"});

//some code here to write blob to temp folder to make nsifile or do some stream stuff to get an nisfileoutputstream?
var blobNSIFILE = ....;

let postData = encodeFormData({
  "webmasterfile": blobNSIFILE,
  "userfile": userfileNSIFILE,
  "username": "Groucho",
  "accountnum": 123456
}, "iso8859-1");

gBrowser.loadOneTab("http://foo.com/submitform.php", {
  inBackground: false,
  postData: postData
});
于 2014-07-30T17:11:54.737 回答