XMLHttpRequest POST formData到一个新的选项卡 [英] XMLHttpRequest POST formData to a new tab
问题描述
因为 loadOneTab()
不可用于 formData
,
- 如何将
formData
过帐到新选项卡? -
$ b
只需要一个简单的来自使用FormData对象的示例:
var formData = new FormData();
formData.append(username,Groucho);
formData.append(accountnum,123456); //数字123456被立即转换为字符串123456
// HTML文件输入用户的选择...
formData.append(userfile,fileInputElement.files [0]);
// JavaScript文件类对象...
var content ='< a id =a>< b id =b>嘿! / b个;< / A>'; //新文件的主体...
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);
说明:
target =_ blank
会将表单数据发布到一个新的选项卡。
同样,如前所述,loadOneTab()
也可以POST数据到一个新的选项卡。
是否可以使用XMLHttpRequest来完成?解决方案
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();
Since,
loadOneTab()
is not available forformData
,- How can
formData
be posted to a new tab? - 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 atarget="_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?解决方案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();
这篇关于XMLHttpRequest POST formData到一个新的选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文 - How can