Extraits de code:Poster des données dans une fenêtre
Un article de MDC.
Cette page est en cours de traduction, son contenu peut donc être incomplet ou contenir des parties en anglais. N'hésitez pas à participer à sa traduction à partir de Code snippets:Post data to window
Cette page contiendra des exemples sur l'envoi de données POST vers un serveur et l'affichage de la réponse sur serveur.
(Besoin de plus d'exemples élaborés)
Sommaire |
[modifier] Preprocessing POST data
The aPostData argument of the (tab)browser.loadURI(), openDialog(), and (tab)browser.loadURIWithFlags() methods expects the POST data in the form of an nsIInputStream because they eventually call nsIWebNavigation.loadURI(). Most of the time, POST data starts as a data string in the form of "name1=data1&name2=data2&...", so you must convert it before passing the data to one of the methods. Here is an example:
var dataString = "name1=data1&name2=data2";
// POST method requests must wrap the encoded text in a MIME
// stream
const Cc = Components.classes;
const Ci = Components.interfaces;
var stringStream = Cc["@mozilla.org/io/string-input-stream;1"].
createInstance(Ci.nsIStringInputStream);
if ("data" in stringStream) // Gecko 1.9 or newer
stringStream.data = dataString;
else // 1.8 or older
stringStream.setData(dataString, dataString.length);
var postData = Cc["@mozilla.org/network/mime-input-stream;1"].
createInstance(Ci.nsIMIMEInputStream);
postData.addHeader("Content-Type", "application/x-www-form-urlencoded");
postData.addContentLength = true;
postData.setData(stringStream);
// postData is ready to be used as aPostData argument
...
[modifier] POSTing data to the current tab
loadURI(aURI, aReferrer, aPostData);
[modifier] POSTing data to a new window
window.openDialog('chrome://browser/content', '_blank', 'all,dialog=no',
aURI, aReferrer, aPostData);