Skip to content

Commit 1952c5d

Browse files
committed
【fix】当请求体为formdata时Content-Type不填充默认是由浏览器确认 review by huangqinghua
1 parent 78b6b0c commit 1952c5d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/common/util/FetchRequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export var FetchRequest = SuperMap.FetchRequest = {
191191
_fetch: function (url, params, options, type) {
192192
options = options || {};
193193
options.headers = options.headers || {};
194-
if (!options.headers['Content-Type']) {
194+
if (!options.headers['Content-Type'] && !FormData.prototype.isPrototypeOf(params)) {
195195
options.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
196196
}
197197
if (options.timeout) {

test/common/util/FetchRequestSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,32 @@ describe('FetchRequest', () => {
8989
timeout: 45000
9090
});
9191
});
92+
it('formdata', () => {
93+
var url = 'http://test.supermap.io';
94+
var data = new FormData();
95+
FetchRequest.post(url, new FormData(), { withCredentials: 'depends' });
96+
expect(fetch).toHaveBeenCalledWith('http://test.supermap.io.json', {
97+
method: 'POST',
98+
body: data,
99+
headers: { },
100+
credentials: 'same-origin',
101+
mode: 'cors',
102+
timeout: 45000
103+
});
104+
});
105+
it('formdata_includeContentType', () => {
106+
var url = 'http://test.supermap.io';
107+
var data = new FormData();
108+
FetchRequest.post(url, new FormData(), { withCredentials: 'depends', headers: { 'Content-Type': 'aaaaa' } });
109+
expect(fetch).toHaveBeenCalledWith('http://test.supermap.io.json', {
110+
method: 'POST',
111+
body: data,
112+
headers: { 'Content-Type': 'aaaaa' },
113+
credentials: 'same-origin',
114+
mode: 'cors',
115+
timeout: 45000
116+
});
117+
});
92118
afterAll(() => {
93119
SuperMap.Util.RequestJSONPPromise.limitLength = defaultval;
94120
setCORS(defaltCors);

0 commit comments

Comments
 (0)