MDNjavascript
XMLHttpRequest Level 2 adds support for the new FormData interface. FormData objects provide a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest send() method.java
XMLHttpRequest 第二版添加了對新接口 FormData 的支持。FormData 能夠很方便地將表單字段和它們的值創建成鍵和值對應的成對形式,而後經過 XMLHttpRequest 的 sent()
方法發送表格數據。瀏覽器
用於新建一個 FormData 對象:app
javascriptvar formData = new FormData(form);
也能夠先建立一個空 FormData 對象,以後再添加對應的鍵和值,append()
方法請看下面。ide
javascriptvar formData = new FormData(); formData.append('username', 'Justin');
本方法用於向已存在的鍵添加新的值,如該鍵不存在,新建之。code
javascriptformData.append(name, value); formData.append(name, value, filename);
將一對鍵和值從 FormData 對象中刪除。orm
javascriptformData.delete(username);
返回給定鍵的第一個值server
javascriptformData.append('username', 'Justin'); formData.append('username', 'Chris'); formData.get(username); // "Justin"
返回給定鍵的全部值對象
javascriptformData.append('username', 'Justin'); formData.append('username', 'Chris'); formData.get(username); // ["Justin", "Chris"]
檢查是否包含給定鍵,返回 true
或 false
接口
javascriptformData.has(username);
設置給定鍵的值
javascriptformData.set(name, value); formData.set(name, value, filename);
來自 MDN:
Desktop
Feature | Chrome | Firfox(Gecko) | Intenet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 7+ | 4.0(2.0) | 10+ | 12+ | 5+ |
append with filename | (Yes) | 22.0(22.0) | ? | ? | ? |
delete, get, getAll, has, set | Behind Flag | Not supported | Not supported | (Yes) | Not supported |
Mobile
Feature | Android | Chrome Android | Firfox Mobile (Gecko) | Firfox OS (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|---|
Basic support | 3.0 | ? | 4.0(2.0) | 1.0.1 | ? | 12+ | ? |
append with filename | ? | ? | 22.0(22.0) | 1.2 | ? | ? | ? |
delete, get, getAll, has, set | (Yes) | (Yes) | Not supported | Not supported | Not supported | (Yes) | Not supported |