Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a post request into a new browser tab using JavaScript / XUL?

I'm trying to open a new browser tab with the results of a POST request. I'm trying to do so using a function containing the following code:

var windowManager = Components.classes["@mozilla.org/appshell/window-mediator;1"]
    .getService(Components.interface
s.nsIWindowMediator);
var browserWindow = windowManager.getMostRecentWindow("navigator:browser");
var browser = browserWindow.getBrowser();
if(browser.mCurrentBrowser.currentURI.spec == "about:blank")
    browserWindow.loadURI(url, null, postData, false);
else
    browser.loadOneTab(url, null, null, postData, false, false);

I'm using a string as url, and JSON data as postData. Is there something I'm doing wrong?

What happens, is a new tab is created, the location shows the URL I want to post to, but the document is blank. The Back, Forward, and Reload buttons are all grayed out on the browser. It seems like it did everything except executed the POST. If I leave the postData parameter off, then it properly runs a GET.

Build identifier: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1

like image 835
aryeh Avatar asked Aug 30 '08 17:08

aryeh


2 Answers

Something which is less Mozilla specific and should work reasonably well with most of the browsers:

  • Create a hidden form with the fields set up the way you need them
  • Make sure that the "target" attribute of the form is set to "_BLANK"
  • Submit the form programatically
like image 168
Grey Panther Avatar answered Oct 21 '22 20:10

Grey Panther


The answer to this was found by shog9. The postData parameter needs to be a nsIMIMEInputStream object as detailed in here.

like image 37
aryeh Avatar answered Oct 21 '22 19:10

aryeh