UNPKG

3.79 kBJavaScriptView Raw
1var Util = require('./util')
2
3// BEGIN(BROWSER)
4
5function open() {
6
7}
8
9function send() {
10
11}
12
13// END(BROWSER)
14
15
16/*
17 from https://github.com/firebug/firebug-lite/blob/master/content/lite/xhr.js
18 http://xhr.spec.whatwg.org/
19 https://github.com/trek/FakeXMLHttpRequest/blob/master/fake_xml_http_request.js
20
21 **需不需要全面重写 XMLHttpRequest?**
22
23 直接修改 xhr.readyState 不生效,所以必须要全面重写 XMLHttpRequest!
24
25 ```javascript
26 readonly attribute unsigned short readyState;
27 ```
28
29 // Event handlers
30 event handler event handler event type
31 onloadstart loadstart
32 onprogress progress
33 onabort abort
34 onerror error
35 onload load
36 ontimeout timeout
37 onloadend loadend
38 onreadystatechange readystatechange
39
40 new window.XMLHttpRequest()
41 new window.ActiveXObject("Microsoft.XMLHTTP")
42
43*/
44
45var FakeXMLHttpRequest = (function() {
46
47 var STATES = {
48 // The object has been constructed.
49 UNSENT: 0,
50 // The open() method has been successfully invoked.
51 OPENED: 1,
52 // All redirects (if any) have been followed and all HTTP headers of the response have been received.
53 HEADERS_RECEIVED: 2,
54 // The response's body is being received.
55 LOADING: 3,
56 // The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).
57 DONE: 4
58 }
59
60 var HTTP_STATUS_CODES = {
61 100: "Continue",
62 101: "Switching Protocols",
63 200: "OK",
64 201: "Created",
65 202: "Accepted",
66 203: "Non-Authoritative Information",
67 204: "No Content",
68 205: "Reset Content",
69 206: "Partial Content",
70 300: "Multiple Choice",
71 301: "Moved Permanently",
72 302: "Found",
73 303: "See Other",
74 304: "Not Modified",
75 305: "Use Proxy",
76 307: "Temporary Redirect",
77 400: "Bad Request",
78 401: "Unauthorized",
79 402: "Payment Required",
80 403: "Forbidden",
81 404: "Not Found",
82 405: "Method Not Allowed",
83 406: "Not Acceptable",
84 407: "Proxy Authentication Required",
85 408: "Request Timeout",
86 409: "Conflict",
87 410: "Gone",
88 411: "Length Required",
89 412: "Precondition Failed",
90 413: "Request Entity Too Large",
91 414: "Request-URI Too Long",
92 415: "Unsupported Media Type",
93 416: "Requested Range Not Satisfiable",
94 417: "Expectation Failed",
95 422: "Unprocessable Entity",
96 500: "Internal Server Error",
97 501: "Not Implemented",
98 502: "Bad Gateway",
99 503: "Service Unavailable",
100 504: "Gateway Timeout",
101 505: "HTTP Version Not Supported"
102 }
103
104 function FakeXMLHttpRequest() {
105 this.readyState = 0
106
107 this.timeout = 0
108
109 this.status = 0
110 this.statusText = ""
111 this.responseType = ""
112 this.response = ""
113 this.responseText = ""
114 this.responseXML = null
115 }
116
117 Util.extend(FakeXMLHttpRequest, STATES)
118 Util.extend(FakeXMLHttpRequest.prototype, STATES)
119
120 // Request
121 Util.extend(FakeXMLHttpRequest.prototype, {
122 open: function open(method, url, async, username, password) {
123
124 },
125 setRequestHeader: function setRequestHeader(name, value) {
126
127 },
128 timeout: 0,
129 withCredentials: false,
130 upload: {},
131 send: send(data),
132 abort: abort()
133 })
134
135 // Response
136 Util.extend(FakeXMLHttpRequest.prototype, {
137 responseURL: '',
138 status: '',
139 statusText: '',
140 getResponseHeader: function getResponseHeader(name) {
141
142 },
143 getAllResponseHeaders: function getAllResponseHeaders() {},
144 overrideMimeType: function overrideMimeType(mime) {},
145 responseType: '', // '', 'text', 'arraybuffer', 'blob', 'document', 'json'
146 response: undefined,
147 responseText: '',
148 responseXML: ''
149 })
150
151 // EventTarget
152 Util.extend(FakeXMLHttpRequest.prototype, {
153 addEventListene: function addEventListene() {},
154 removeEventListener: function removeEventListener() {},
155 dispatchEvent: function dispatchEvent() {}
156 })
157
158 return FakeXMLHttpRequest
159
160})()