0

Since, loadOneTab() is not available for formData,

  1. How can formData be posted to a new tab?
  2. How can the above new tab foreground/background status be set?

Just a smaple example from 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);

Clarification:
Normal HTML form with a target="_blank" will POST the form data to a new tab.
Similarly, as mentioned, loadOneTab() can also POST data to a new tab.
Is it possible to do so with XMLHttpRequest?

erosman
  • 7,094
  • 7
  • 27
  • 46
  • What do you mean, "posted to a new tab?" If you're opening a new window or tab, it doesn't really use XHR, it's a new window. – Palpatim Jul 30 '14 at 15:19
  • @Palpatim : clarification added. @nmaier: Although related, not the same. here the question is about `XMLHttpRequest` but that one was about `loadOneTab()`. You have told me in the past to make a new question and not to add additional questions to the existing one. – erosman Jul 30 '14 at 15:37
  • I think I see why the `encodeFormData` from @nmaier's solution is not working. Is `fileInputElement.files[0]` instance of `Ci.nsIFile`? It might be instance of that `DOM File`? – Blagoh Jul 30 '14 at 16:37
  • As a last resort: can you XHR the request and update the document of the tab to reflect the XHR result? – Blagoh Jul 30 '14 at 16:40
  • So what I would do is use `encodeFormData` trick from @nmaier's post but I would change the `blob` to be `nsIFile` and same with `fileInputElement.files[0]` than it should work no? – Blagoh Jul 30 '14 at 17:05

2 Answers2

1

XHR has absolutely nothing to do with tabs. If you really want to XHR it, then you should take the returned source and update document of the target tab with it.

Otherwise I would just use loadOneTab: I would think something like this where things are turned into nsIFile:

Import encodeFormData function form here: 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
});
Community
  • 1
  • 1
Blagoh
  • 1,225
  • 1
  • 14
  • 29
  • This here uses XHR to send form data: http://stackoverflow.com/a/22037120/3791822 – Blagoh Jul 30 '14 at 17:22
  • That's really very weird. You're running that XHR from xpcom scope, there is no domain with it. That example does load the resulting page but it doesnt load it in a tab, it's held in `xhr.responseText` when `xhr.readystate` is 4 in the `xhr.onreadystatechange` event. So get your tab, or create a new tab, then set its `window.location = 'data;html/text:' + xhr.responseText;` – Blagoh Jul 30 '14 at 17:34
  • I was trying codes from the Scratch Pad in Browser environment. Let me do some more testing :) – erosman Jul 30 '14 at 17:39
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();
Blagoh
  • 1,225
  • 1
  • 14
  • 29