Skip to content

Commit 9132861

Browse files
authored
fix: access xhr.status after xhr.readyState becomes DONE (fix #45) (#46)
1 parent b04e514 commit 9132861

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

ajax/index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ function getComputedOptions(defaultOptions, customOptions) {
126126
return options;
127127
}
128128

129-
var ENCODED_SPACE_REGEXP = /%20/g;
130-
var QS_DELIM_REGEXP = /\?/;
131-
132129
function validateStatus(status) {
133130
return status >= 200 && status < 300;
134131
}
@@ -173,9 +170,16 @@ function parseJSONData(data) {
173170
return result;
174171
}
175172

173+
var REQUEST_DONE = 4;
174+
176175
function handleReadyStateChange(xhr, options) {
177-
var readyState = xhr.readyState,
178-
status = xhr.status,
176+
var readyState = xhr.readyState;
177+
178+
if (readyState != REQUEST_DONE) {
179+
return;
180+
}
181+
182+
var status = xhr.status,
179183
statusText = xhr.statusText,
180184
responseText = xhr.responseText;
181185
var success = options.success,
@@ -184,10 +188,6 @@ function handleReadyStateChange(xhr, options) {
184188
reject = options.reject,
185189
complete = options.complete;
186190

187-
if (readyState != XMLHttpRequest.DONE) {
188-
return;
189-
}
190-
191191
if (validateStatus(status)) {
192192
var contentType = xhr.getResponseHeader('Content-Type');
193193
var data = responseText;
@@ -215,6 +215,8 @@ function handleReadyStateChange(xhr, options) {
215215
});
216216
}
217217

218+
var QS_DELIM_REGEXP = /\?/;
219+
218220
function open(xhr, options) {
219221
var url = options.url,
220222
method = options.method,
@@ -259,6 +261,8 @@ function applyConfig(xhr, options) {
259261
xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
260262
}
261263

264+
var ENCODED_SPACE_REGEXP = /%20/g;
265+
262266
function send(xhr, options) {
263267
var method = options.method,
264268
serializer = options.serializer,

ajax/index.mjs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,6 @@ function getComputedOptions(defaultOptions, customOptions) {
160160
return options;
161161
}
162162

163-
const ENCODED_SPACE_REGEXP = /%20/g;
164-
const QS_DELIM_REGEXP = /\?/;
165-
166163
function validateStatus(status) {
167164
return status >= 200 && status < 300;
168165
}
@@ -204,15 +201,19 @@ function parseJSONData(data) {
204201
return result;
205202
}
206203

204+
const REQUEST_DONE = 4;
205+
207206
function handleReadyStateChange(xhr, options) {
208-
const { readyState, status, statusText, responseText } = xhr;
209-
const { success, resolve, error, reject, complete } = options;
207+
const { readyState } = xhr;
210208

211209
// eslint-disable-next-line eqeqeq
212-
if (readyState != XMLHttpRequest.DONE) {
210+
if (readyState != REQUEST_DONE) {
213211
return;
214212
}
215213

214+
const { status, statusText, responseText } = xhr;
215+
const { success, resolve, error, reject, complete } = options;
216+
216217
if (validateStatus(status)) {
217218
const contentType = xhr.getResponseHeader('Content-Type');
218219
let data = responseText;
@@ -257,6 +258,8 @@ function handleReadyStateChange(xhr, options) {
257258
executeCallback(complete, { status, statusText });
258259
}
259260

261+
const QS_DELIM_REGEXP = /\?/;
262+
260263
function open(xhr, options) {
261264
const { url, method, serializer, params } = options;
262265

@@ -298,6 +301,8 @@ function applyConfig(xhr, options) {
298301
xhr.setRequestHeader('x-requested-with', 'XMLHttpRequest');
299302
}
300303

304+
const ENCODED_SPACE_REGEXP = /%20/g;
305+
301306
function send(xhr, options) {
302307
const {
303308
method,

0 commit comments

Comments
 (0)