問題描述
FormData 在 IE 8/9 中不存在,但我需要這些瀏覽器中的功能.有一個(gè)很好的后備方案嗎?
FormData does not exist in IE 8/9 but I need that functionality in those browsers. Is there a nice fallback for this?
我會(huì)嘗試通過 json 數(shù)據(jù)發(fā)送,但我需要將文件傳遞給服務(wù)器.我將此文件附加到現(xiàn)代瀏覽器中的 formData
并提交一個(gè) XHR
請(qǐng)求.因?yàn)?FormData
在 IE 8/9 中不存在,這顯然失敗了.
I would try to send over json data, but I need to pass over a file to the server. I append this file to the formData
in modern browsers and just submit an XHR
request. Because FormData
does not exist in IE 8/9 this obviously fails.
// I cant seem to get this to work with a file.
$.ajax({
url: '/genericHandlers/UploadDocsFile.ashx',
type: "POST",
data: model.toJSON(),
contentType: 'application/json'
}).done(function (data) {
log('stuff happened!');
});
也許另一種方法是在 js 中創(chuàng)建一個(gè)假表單對(duì)象,然后將數(shù)據(jù)附加到該對(duì)象?
Maybe an alternative is to create a fake form object in js then append the data to that?
推薦答案
我只知道一種可能的解決方案,但這并不是 IE 真正的 1-1 后備方案.沒有可能用于發(fā)送文件的通信 API,因?yàn)槟鸁o法在舊瀏覽器中綁定輸入字段,例如在使用 FormData 的現(xiàn)代瀏覽器中.但是您可以使用 iframe 發(fā)送整個(gè)表單.對(duì)于這種情況,您可以使用支持 XHR DataForm 和 iframe 的 jquery.form 插件(當(dāng)瀏覽器不支持 FormData API 時(shí)使用 iframe 發(fā)送數(shù)據(jù)).
I know only one possible solution, but it's not really 1-1 fallback for IEs. There are no possible communication API for sending files, because you cannot bind input fields in old browsers, like in a modern ones using FormData. But you can send whole form using an iframe. For this case you can use jquery.form plugin that support XHR DataForm and iframe (data sends with iframe when browser do not FormData API support).
這篇關(guān)于IE 8/9 中 FormData 的后備的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!