forked from RubyLouvre/mass-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.js
710 lines (693 loc) · 30.2 KB
/
ajax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//=========================================
// 数据交互模块
//==========================================
//var reg = /^[^\u4E00-\u9FA5]*$/;
define("ajax", ["mass", "flow"], function($) {
var global = this,
DOC = global.document,
r20 = /%20/g,
rCRLF = /\r?\n/g,
encode = encodeURIComponent,
decode = decodeURIComponent,
rheaders = /^(.*?):[ \t]*([^\r\n]*)\r?$/mg,
// IE的换行符不包含 \r
rlocalProtocol = /^(?:about|app|app-storage|.+-extension|file|res|widget):$/,
rnoContent = /^(?:GET|HEAD)$/,
rquery = /\?/,
rurl = /^([\w.+-]+:)(?:\/\/([^\/?#:]*)(?::(\d+)|)|)/,
//在IE下如果重置了document.domain,直接访问window.location会抛错,但用document.URL就ok了
//http://www.cnblogs.com/WuQiang/archive/2012/09/21/2697474.html
curl = DOC.URL,
segments = rurl.exec(curl.toLowerCase()) || [],
//http://www.cnblogs.com/rubylouvre/archive/2010/04/20/1716486.html
s = ["XMLHttpRequest", "ActiveXObject('Msxml2.XMLHTTP.6.0')", "ActiveXObject('Msxml2.XMLHTTP.3.0')", "ActiveXObject('Msxml2.XMLHTTP')", "ActiveXObject('Microsoft.XMLHTTP')"];
if(!+"\v1") {
var v = DOC.documentMode;
s[0] = v == 8 ? "XDomainRequest" : location.protocol === "file:" ? "!" : s[0]
}
for(var i = 0, axo; axo = s[i++];) {
try {
if(eval("new " + axo)) {
$.xhr = new Function("return new " + axo);
break;
}
} catch(e) {}
}
var accepts = {
xml: "application/xml, text/xml",
html: "text/html",
text: "text/plain",
json: "application/json, text/javascript",
script: "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript",
"*": ["*/"] + ["*"] //避免被压缩掉
},
defaults = {
type: "GET",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
async: true,
jsonp: "callback"
};
//将data转换为字符串,type转换为大写,添加hasContent,crossDomain属性,如果是GET,将参数绑在URL后面
function setOptions(opts) {
opts = $.Object.merge({}, defaults, opts);
if(opts.crossDomain == null) { //判定是否跨域
var parts = rurl.exec(opts.url.toLowerCase());
opts.crossDomain = !! (parts && (parts[1] !== segments[1] || parts[2] !== segments[2] || (parts[3] || (parts[1] === "http:" ? 80 : 443)) != (segments[3] || (segments[1] === "http:" ? 80 : 443))));
}
if(opts.data && opts.data !== "string") {
opts.data = $.param(opts.data);
}
opts.url = opts.url.replace(/#.*$/, "").replace(/^\/\//, segments[1] + "//");
opts.type = opts.type.toUpperCase();
opts.hasContent = !rnoContent.test(opts.type); //是否为post请求
if(!opts.hasContent) {
if(opts.data) { //如果为GET请求,则参数依附于url上
opts.url += (rquery.test(opts.url) ? "&" : "?") + opts.data;
}
if(opts.cache === false) { //添加时间截
opts.url += (rquery.test(opts.url) ? "&" : "?") + "_time=" + Date.now();
}
}
return opts;
}
"get,post".replace($.rword, function(method) {
$[method] = function(url, data, callback, type) {
if($.isFunction(data)) {
type = type || callback;
callback = data;
data = undefined;
}
return $.ajax({
type: method,
url: url,
data: data,
success: callback,
dataType: type
});
};
});
function isValidParamValue(val) {
var t = typeof val; // If the type of val is null, undefined, number, string, boolean, return true.
return val == null || (t !== 'object' && t !== 'function');
}
$.mix($, {
getScript: function(url, callback) {
return $.get(url, null, callback, "script");
},
getJSON: function(url, data, callback) {
return $.get(url, data, callback, "jsonp");
},
/**无刷新上传
* @param {String} url 提交地址
* @param {HTMLElement} 元素
* @param {Object} data 普通对象(用于添加额外参数)
* @param {Function} 成功回调,它的参数依次是
* 1. data returned from this request with type specified by dataType<br/>
* 2. status of this request with type String<br/>
* 3. XhrObject of this request , for details {@link IO.XhrObject}
* @param {String} [dataType] 注明要返回何种数据类型("xml" or "json" or "text")
* @returns {IO.XhrObject}
*/
upload: function(url, form, data, callback, dataType) {
if($.isFunction(data)) {
dataType = callback;
callback = data;
data = undefined;
}
return $.ajax({
url: url,
type: 'post',
dataType: dataType,
form: form,
data: data,
success: callback
});
},
//将一个对象转换为字符串
param: function(json, bracket) {
if(!$.isPlainObject(json)) {
return "";
}
bracket = typeof serializeArray == "boolean" ? bracket : !0;
var buf = [],
key, val;
for(key in json) {
if(json.hasOwnProperty(key)) {
val = json[key];
key = encode(key);
if(isValidParamValue(val)) { //只处理基本数据类型,忽略空数组,函数,正则,日期,节点等
buf.push(key, "=", encode(val + ""), "&");
} else if(Array.isArray(val) && val.length) { //不能为空数组
for(var i = 0, n = val.length; i < n; i++) {
if(isValidParamValue(val[i])) {
buf.push(key, (bracket ? encode("[]") : ""), "=", encode(val[i] + ""), "&");
}
}
}
}
}
buf.pop();
return buf.join("").replace(r20, "+");
},
//将一个字符串转换为对象
//$.deparam = jq_deparam = function( params, coerce ) {
//https://github.com/cowboy/jquery-bbq/blob/master/jquery.ba-bbq.js
unparam: function(url, query) {
var json = {};
if(!url || !$.type(url, "String")) {
return json
}
url = url.replace(/^[^?=]*\?/ig, '').split('#')[0]; //去除网址与hash信息
//考虑到key中可能有特殊符号如“[].”等,而[]却有是否被编码的可能,所以,牺牲效率以求严谨,就算传了key参数,也是全部解析url。
var pairs = url.split("&"),
pair, key, val, i = 0,
len = pairs.length;
for(; i < len; ++i) {
pair = pairs[i].split("=");
key = decode(pair[0]);
try {
val = decode(pair[1] || "");
} catch(e) {
$.log(e + "decodeURIComponent error : " + pair[1], 3);
val = pair[1] || "";
}
key = key.replace(/\[\]$/, "") //如果参数名以[]结尾,则当作数组
var item = json[key];
if(item === void 0) {
json[key] = val; //第一次
} else if(Array.isArray(item)) {
item.push(val); //第三次或三次以上
} else {
json[key] = [item, val]; //第二次,将它转换为数组
}
}
return query ? json[query] : json;
},
serialize: function(form) { //表单元素变字符串
var json = {};
// 不直接转换form.elements,防止以下情况: <form > <input name="elements"/><input name="test"/></form>
$.filter(form || [], function(el) {
return el.name && !el.disabled && (el.checked === true || /radio|checkbox/.test(el.type))
}).forEach(function(el) {
var val = $(el).val(),
vs;
val = Array.isArray(val) ? val : [val];
val = val.map(function(v) {
return v.replace(rCRLF, "\r\n");
});
// 全部搞成数组,防止同名
vs = json[el.name] || (json[el.name] = []);
vs.push.apply(vs, val);
});
return $.param(json, false); // 名值键值对序列化,数组元素名字前不加 []
}
});
/*=============================================================================================
从这里开始是数据交互模块的核心,包含一个ajax方法,ajaxflow对象,传送器集合,转换器集合
=============================================================================================*/
var ajaxflow = new $.Flow
var transports = {} //传送器,我们可以通过XMLHttpRequest, Script, Iframe与后端
var converters = { //转换器,返回用户想要做的数据(从原始返回值中提取加工)
text: function(xhr, text, xml) {
return text != undefined ? text : ("xml" in xml ? xml.xml : new XMLSerializer().serializeToString(xml));
},
xml: function(xhr, text, xml) {
return xml != undefined ? xml : $.parseXML(text);
},
html: function(xhr, text, xml) {
return $.parseHTML(text);
},
json: function(xhr, text, xml) {
return $.parseJSON(text);
},
script: function(xhr, text, xml) {
$.parseJS(text);
}
}
var ajax = $.ajax = function(opts) {
if(!opts || !opts.url) {
$.error("参数必须为Object并且拥有url属性" );
}
opts = setOptions(opts); //规整化参数对象
//创建一个伪XMLHttpRequest,能处理complete,success,error等多投事件
var dummyXHR = new $.XHR(opts),
dataType = opts.dataType;
if(opts.form && opts.form.nodeType === 1) {
dataType = "iframe";
} else if(dataType == "jsonp") {
if(opts.crossDomain) { // opts.crossDomain &&
$.log("使用script发出JSONP请求")
ajaxflow.fire("start", dummyXHR, opts.url, opts.jsonp, opts.jsonpCallback); //用于jsonp请求
dataType = "script"
} else {
dataType = dummyXHR.options.dataType = "json";
}
}
var transportContructor = transports[dataType] || transports._default,
transport = new transportContructor();
transport.dummyXHR = dummyXHR;
dummyXHR.transport = transport;
if(opts.contentType) {
dummyXHR.setRequestHeader("Content-Type", opts.contentType);
}
//添加dataType所需要的Accept首部
dummyXHR.setRequestHeader("Accept", accepts[dataType] ? accepts[dataType] + ", */*; q=0.01" : accepts["*"]);
for(var i in opts.headers) {
dummyXHR.setRequestHeader(i, opts.headers[i]);
}
"complete success error".replace($.rword, function(name) {
if(typeof opts[name] === "function") {
dummyXHR.addEventListener(name, opts[name])
delete opts[name];
}
});
dummyXHR.readyState = 1;
// 处理超时
if(opts.async && opts.timeout > 0) {
dummyXHR.timeoutID = setTimeout(function() {
dummyXHR.abort("timeout");
}, opts.timeout);
}
try {
dummyXHR.state = 1; //已发送
transport.request();
} catch(e) {
if(dummyXHR.status < 2) {
dummyXHR.dispatch(-1, e);
} else {
$.log(e);
}
}
return dummyXHR;
}
ajax.isLocal = rlocalProtocol.test(segments[1]);
/**
* XHR类,用于模拟原生XMLHttpRequest的所有行为
* var ajax = new(self.XMLHttpRequest||ActiveXObject)("Microsoft.XMLHTTP")
* ajax.onreadystatechange = function(){
* if (ajax.readyState==4 && ajax.status==200){
* alert(ajax.responseText)
* }
* }
* ajax.open("POST", url, true);
* ajax.send("key=val&key1=val2");
*/
$.XHR = $.factory({
inherit: $.Flow,
init: function(opts) {
$.mix(this, {
responseData: null,
timeoutID: null,
responseText: null,
responseXML: null,
statusText: null,
transport: null,
responseHeadersString: "",
responseHeaders: {},
requestHeaders: {},
readyState: 0,
//internal state
state: 0,
status: 0
});
this.setOptions("options", opts); //创建一个options保存原始参数
},
setRequestHeader: function(name, value) {
this.requestHeaders[name] = value;
return this;
},
getAllResponseHeaders: function() {
return this.state === 2 ? this.responseHeadersString : null;
},
getResponseHeader: function(name, match) {
if(this.state === 2) {
while((match = rheaders.exec(this.responseHeadersString))) {
this.responseHeaders[match[1]] = match[2];
}
match = this.responseHeaders[name];
}
return match === undefined ? null : match;
},
overrideMimeType: function(type) {
// 只有在没有发出请求前才能重写 content-type 首部
if(!this.state) {
this.mimeType = type;
}
return this;
},
toString: function() {
return "[object XMLHttpRequest]"
},
// 中止请求
abort: function(statusText) {
statusText = statusText || "abort";
if(this.transport) {
this.transport.respond(0, 1);
}
this.dispatch(0, statusText);
return this;
},
/**
* 用于派发success,error,complete等回调
* http://www.cnblogs.com/rubylouvre/archive/2011/05/18/2049989.html
* @param {Number} status 状态码
* @param {String} statusText 对应的扼要描述
*/
dispatch: function(status, statusText) {
// 只能执行一次,防止重复执行
if(this.state == 2) { //2:已执行回调
return;
}
this.state = 2;
this.readyState = 4;
var eventType = "error";
if(status >= 200 && status < 300 || status == 304) {
if(status == 304) {
statusText = "notmodified";
eventType = "success";
} else {
try {
var dataType = this.options.dataType || this.options.mimeType || this.nativeXHR && this.nativeXHR.responseType;
if(!dataType) { //如果没有指定dataType,则根据mimeType或Content-Type进行揣测
dataType = this.getResponseHeader("Content-Type") || "";
dataType = dataType.match(/json|xml|script|html/) || ["text"];
dataType = dataType[0]
}
this.responseData = converters[dataType](this, this.responseText, this.responseXML);
eventType = statusText = "success";
$.log("dummyXHR.dispatch success");
} catch(e) {
$.log("dummyXHR.dispatch parsererror")
statusText = "parsererror : " + e;
}
}
} else if(status < 0) {
status = 0;
}
this.status = status;
this.statusText = statusText;
if(this.timeoutID) {
clearTimeout(this.timeoutID);
delete this.timeoutID;
}
// 到这要么成功,调用success, 要么失败,调用 error, 最终都会调用 complete
this.fire(eventType, this.responseData, statusText);
ajaxflow.fire(eventType);
this.fire("complete", this.responseData, statusText);
ajaxflow.fire("complete");
delete this.transport;
}
});
if($.xhr) {
var nativeXHR = new $.xhr,
allowCrossDomain = false;
if("withCredentials" in nativeXHR) {
allowCrossDomain = true;
}
//【XMLHttpRequest】传送器
transports._default = $.factory({
//发送请求
request: function() {
var dummyXHR = this.dummyXHR,
options = dummyXHR.options,
i;
$.log("XhrTransport.sending.....");
if(options.crossDomain && !allowCrossDomain) {
$.error("本浏览器不支持crossdomain xhr")
}
var nativeXHR = this.nativeXHR = new $.xhr,
self = this;
if(options.username) {
nativeXHR.open(options.type, options.url, options.async, options.username, options.password);
} else {
nativeXHR.open(options.type, options.url, options.async);
}
// 如果支持overrideMimeTypeAPI
if(dummyXHR.mimeType && nativeXHR.overrideMimeType) {
nativeXHR.overrideMimeType(dummyXHR.mimeType);
}
if(!options.crossDomain && !dummyXHR.requestHeaders["X-Requested-With"]) {
dummyXHR.requestHeaders["X-Requested-With"] = "XMLHttpRequest";
}
try {
for(i in dummyXHR.requestHeaders) {
nativeXHR.setRequestHeader(i, dummyXHR.requestHeaders[i]);
}
} catch(e) {
$.log(" nativeXHR setRequestHeader occur error ");
}
nativeXHR.send(options.hasContent && options.data || null);
//在同步模式中,IE6,7可能会直接从缓存中读取数据而不会发出请求,因此我们需要手动发出请求
if(!options.async || nativeXHR.readyState == 4) {
this.respond();
} else {
if(nativeXHR.onerror === null) { //如果支持onerror, onload新API
nativeXHR.onload = nativeXHR.onerror = function(e) {
this.readyState = 4; //IE9
this.status = e.type === "load" ? 200 : 500;
self.respond();
};
} else {
nativeXHR.onreadystatechange = function() {
self.respond();
}
}
}
},
//用于获取原始的responseXMLresponseText 修正status statusText
//第二个参数为1时中止清求
respond: function(event, abort) {
// 如果网络问题时访问XHR的属性,在FF会抛异常
// http://helpful.knobs-dials.com/index.php/Component_returned_failure_code:_0x80040111_(NS_ERROR_NOT_AVAILABLE)
var nativeXHR = this.nativeXHR,
dummyXHR = this.dummyXHR,
detachEvent = false;
try {
if(abort || nativeXHR.readyState == 4) {
detachEvent = true;
if(abort) {
if(nativeXHR.readyState !== 4) { // 完成以后 abort 不要调用
//IE的XMLHttpRequest.abort实现于 MSXML 3.0+
//http://blogs.msdn.com/b/xmlteam/archive/2006/10/23/using-the-right-version-of-msxml-in-internet-explorer.aspx
nativeXHR.abort();
}
} else {
var status = nativeXHR.status,
xml = nativeXHR.responseXML;
dummyXHR.responseHeadersString = nativeXHR.getAllResponseHeaders();
// Construct response list
if(xml && xml.documentElement /* #4958 */ ) {
dummyXHR.responseXML = xml;
}
dummyXHR.responseText = nativeXHR.responseText;
//火狐在跨城请求时访问statusText值会抛出异常
try {
var statusText = nativeXHR.statusText;
} catch(e) {
$.log("xhr statustext error : " + e, 3);
statusText = "";
}
//用于处理特殊情况,如果是一个本地请求,只要我们能获取数据就假当它是成功的
if(!status && ajax.isLocal && !dummyXHR.options.crossDomain) {
status = dummyXHR.responseText ? 200 : 404;
//IE有时会把204当作为1223
//returning a 204 from a PUT request - IE seems to be handling the 204 from a DELETE request okay.
} else if(status === 1223) {
status = 204;
}
dummyXHR.dispatch(status, statusText);
}
}
} catch(firefoxAccessException) {
detachEvent = true;
$.log(firefoxAccessException);
if(!abort) {
dummyXHR.dispatch(-1, firefoxAccessException + "");
}
} finally {
if(detachEvent) {
nativeXHR.onerror = nativeXHR.onload = nativeXHR.onreadystatechange = $.noop;
}
}
}
});
}
//【script节点】传送器,只用于跨域的情况
transports.script = $.factory({
request: function() {
var self = this,
dummyXHR = self.dummyXHR,
options = dummyXHR.options,
head = $.head,
script = self.script = DOC.createElement("script");
$.log("ScriptTransport.sending.....");
if(options.charset) {
script.charset = options.charset;
}
//当script的资源非JS文件时,发生的错误不可捕获
script.onerror = script[script.uniqueID ? "onreadystatechange" : "onload"] = function(e) {
e = e || event;
self.respond((e.type || "error").toLowerCase()); // firefox onerror 没有 type ?!
};
script.src = options.url
head.insertBefore(script, head.firstChild);
},
respond: function(event, isAbort) {
var node = this.script,
dummyXHR = this.dummyXHR;
// 防止重复调用,成功后 abort
if(!node) {
return;
}
if(isAbort || /loaded|complete|undefined/i.test(node.readyState) || event == "error") {
node.onerror = node.onload = node.onreadystatechange = null;
var parent = node.parentNode;
if(parent && parent.nodeType === 1) {
parent.removeChild(node);
delete this.script;
}
//如果没有中止请求并没有报错
if(!isAbort && event != "error") {
dummyXHR.dispatch(200, "success");
}
// 非 ie<9 可以判断出来
else if(event == "error") {
dummyXHR.dispatch(500, "scripterror");
}
}
}
});
//http://www.decimage.com/web/javascript-cross-domain-solution-with-jsonp.html
//JSONP请求,借用【script节点】传送器
converters["jsonp"] = function(xhr) {
var json = $[xhr.jsonp];
delete $[xhr.jsonp];
return json;
}
ajaxflow.bind("start", function(e, dummyXHR, url, jsonp, jsonpCallback) {
$.log("jsonp start...");
var namespace = DOC.URL.replace(/(#.+|\W)/g, '');
jsonpCallback = dummyXHR.jsonp = jsonpCallback || "jsonp" + dummyXHR.uuid().replace(/-/g, "");
dummyXHR.options.url = url + (rquery.test(url) ? "&" : "?") + jsonp + "=" + namespace + "." + jsonpCallback;
dummyXHR.options.dataType = "jsonp";
//将后台返回的json保存在惰性函数中
global[namespace][jsonpCallback] = function(json) {
$[jsonpCallback] = json;
};
});
function createIframe(dummyXHR, transport) {
var id = "iframe-upload-" + dummyXHR.uniqueID;
var iframe = $.parseHTML("<iframe " + " id='" + id + "'" + " name='" + id + "'" + " style='position:absolute;left:-9999px;top:-9999px;/>").firstChild;
iframe.transport = transport;
return(DOC.body || DOC.documentElement).insertBefore(iframe, null);
}
function addDataToForm(data, form, serializeArray) {
data = $.unparam(data);
var ret = [],
d, isArray, vs, i, e;
for(d in data) {
isArray = Array.isArray(data[d]);
vs = isArray ? data[d] : [data[d]]
// 数组和原生一样对待,创建多个同名输入域
for(i = 0; i < vs.length; i++) {
e = DOC.createElement("input");
e.type = 'hidden';
e.name = d + (isArray && serializeArray ? "[]" : "");
e.value = vs[i];
form.appendChild(e)
ret.push(e);
}
}
return ret;
}
//【iframe】传送器,专门用于上传
//http://www.profilepicture.co.uk/tutorials/ajax-file-upload-xmlhttprequest-level-2/ 上传
transports.iframe = $.factory({
request: function() {
var dummyXHR = this.dummyXHR,
options = dummyXHR.options,
form = options.form
//form.enctype的值
//1:application/x-www-form-urlencoded 在发送前编码所有字符(默认)
//2:multipart/form-data 不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
//3:text/plain 空格转换为 "+" 加号,但不对特殊字符编码。
this.backups = {
target: form.target || "",
action: form.action || "",
enctype: form.enctype,
method: form.method
};
var iframe = createIframe(dummyXHR, this);
//必须指定method与enctype,要不在FF报错
//“表单包含了一个文件输入元素,但是其中缺少 method=POST 以及 enctype=multipart/form-data,所以文件将不会被发送。”
// 设置target到隐藏iframe,避免整页刷新
form.target = "iframe-upload-" + dummyXHR.uniqueID;
form.action = options.url;
form.method = "POST";
form.enctype = "multipart/form-data";
this.fields = options.data ? addDataToForm(options.data, form) : [];
this.form = form; //一个表单元素
$.log("iframe transport...");
setTimeout(function() {
$(iframe).bind("load error", this.respond);
form.submit();
});
},
respond: function(event) {
var iframe = this,
transport = iframe.transport;
// 防止重复调用 , 成功后 abort
if(!transport) {
return;
}
$.log("transports.iframe respond")
var form = transport.form,
eventType = event.type,
dummyXHR = transport.dummyXHR;
iframe.transport = undefined;
if(eventType == "load") {
var doc = iframe.contentDocument ? iframe.contentDocument : window.frames[iframe.id].document;
var iframeDoc = iframe.contentWindow.document;
if(doc.XMLDocument) {
dummyXHR.responseXML = doc.XMLDocument;
} else if(doc.body) {
// response is html document or plain text
dummyXHR.responseText = doc.body.innerHTML;
dummyXHR.responseXML = iframeDoc;
//当,MIME为"text/plain",浏览器会把文本放到一个PRE标签中
if(doc.body.firstChild && doc.body.firstChild.nodeName.toUpperCase() == 'PRE') {
dummyXHR.responseText = doc.body.firstChild.firstChild.nodeValue;
}
} else {
// response is a xml document
dummyXHR.responseXML = doc;
}
dummyXHR.dispatch(200, "success");
} else if(eventType == 'error') {
dummyXHR.dispatch(500, "error");
}
for(var i in transport.backups) {
form[i] = transport.backups[i];
}
//还原form的属性
transport.fields.forEach(function(elem) {
elem.parentNode.removeChild(elem);
});
$(iframe).unbind("load", transport.respond).unbind("error", transport.respond);
iframe.clearAttributes && iframe.clearAttributes();
setTimeout(function() {
// Fix busy state in FF3
iframe.parentNode.removeChild(iframe);
$.log("iframe.parentNode.removeChild(iframe)")
});
}
});
return $;
});
/**
2011.8.31
将会传送器的abort方法上传到$.XHR.abort去处理
修复serializeArray的bug
对XMLHttpRequest.abort进行try...catch
2012.3.31 v2 大重构,支持XMLHttpRequest Level2
*/