mirror of
https://github.com/tenrok/axios.git
synced 2026-05-24 14:04:14 +03:00
Merge pull request #185 from jtangelder/improve-ie
Improve XDomainRequest implementation
This commit is contained in:
+29
-36
@@ -1,13 +1,10 @@
|
||||
'use strict';
|
||||
|
||||
/*global ActiveXObject:true*/
|
||||
|
||||
var utils = require('./../utils');
|
||||
var buildURL = require('./../helpers/buildURL');
|
||||
var parseHeaders = require('./../helpers/parseHeaders');
|
||||
var transformData = require('./../helpers/transformData');
|
||||
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
|
||||
var ieVersion = require('./../helpers/ieVersion');
|
||||
var btoa = window.btoa || require('./../helpers/btoa');
|
||||
|
||||
module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
@@ -18,15 +15,12 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
delete requestHeaders['Content-Type']; // Let the browser set it
|
||||
}
|
||||
|
||||
var Adapter = (XMLHttpRequest || ActiveXObject);
|
||||
var loadEvent = 'onreadystatechange';
|
||||
var xDomain = false;
|
||||
var request = new XMLHttpRequest();
|
||||
|
||||
// For IE 8/9 CORS support
|
||||
if (ieVersion() <= 9 && !isURLSameOrigin(config.url) && window.XDomainRequest) {
|
||||
Adapter = window.XDomainRequest;
|
||||
loadEvent = 'onload';
|
||||
xDomain = true;
|
||||
// Only supports POST and GET calls and doesn't returns the response headers.
|
||||
if (window.XDomainRequest && !('withCredentials' in request) && !isURLSameOrigin(config.url)) {
|
||||
request = new window.XDomainRequest();
|
||||
}
|
||||
|
||||
// HTTP basic authentication
|
||||
@@ -36,38 +30,37 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
requestHeaders.Authorization = 'Basic ' + btoa(username + ':' + password);
|
||||
}
|
||||
|
||||
// Create the request
|
||||
var request = new Adapter('Microsoft.XMLHTTP');
|
||||
request.open(config.method.toUpperCase(), buildURL(config.url, config.params, config.paramsSerializer), true);
|
||||
|
||||
// Set the request timeout in MS
|
||||
request.timeout = config.timeout;
|
||||
|
||||
// Listen for ready state
|
||||
request[loadEvent] = function handleReadyState() {
|
||||
if (request && (request.readyState === 4 || xDomain)) {
|
||||
// Prepare the response
|
||||
var responseHeaders = xDomain ? null : parseHeaders(request.getAllResponseHeaders());
|
||||
var responseData = ['text', ''].indexOf(config.responseType || '') !== -1 ? request.responseText : request.response;
|
||||
var response = {
|
||||
data: transformData(
|
||||
responseData,
|
||||
responseHeaders,
|
||||
config.transformResponse
|
||||
),
|
||||
status: request.status,
|
||||
statusText: request.statusText,
|
||||
headers: responseHeaders,
|
||||
config: config
|
||||
};
|
||||
// Resolve or reject the Promise based on the status
|
||||
((request.status >= 200 && request.status < 300) || (xDomain && request.responseText) ?
|
||||
resolve :
|
||||
reject)(response);
|
||||
|
||||
// Clean up request
|
||||
request = null;
|
||||
request.onload = function handleLoad() {
|
||||
if (!request) {
|
||||
return;
|
||||
}
|
||||
// Prepare the response
|
||||
var responseHeaders = 'getAllResponseHeaders' in request ? parseHeaders(request.getAllResponseHeaders()) : null;
|
||||
var responseData = ['text', ''].indexOf(config.responseType || '') !== -1 ? request.responseText : request.response;
|
||||
var response = {
|
||||
data: transformData(
|
||||
responseData,
|
||||
responseHeaders,
|
||||
config.transformResponse
|
||||
),
|
||||
status: request.status,
|
||||
statusText: request.statusText,
|
||||
headers: responseHeaders,
|
||||
config: config
|
||||
};
|
||||
// Resolve or reject the Promise based on the status
|
||||
((request.status >= 200 && request.status < 300) || (!('status' in request) && request.responseText) ?
|
||||
resolve :
|
||||
reject)(response);
|
||||
|
||||
// Clean up request
|
||||
request = null;
|
||||
};
|
||||
|
||||
// Add xsrf header
|
||||
@@ -87,7 +80,7 @@ module.exports = function xhrAdapter(resolve, reject, config) {
|
||||
}
|
||||
|
||||
// Add headers to the request
|
||||
if (!xDomain) {
|
||||
if ('setRequestHeader' in request) {
|
||||
utils.forEach(requestHeaders, function setRequestHeader(val, key) {
|
||||
if (!requestData && key.toLowerCase() === 'content-type') {
|
||||
// Remove Content-Type if data is undefined
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* https://gist.github.com/padolsey/527683
|
||||
*
|
||||
* A short snippet for detecting versions of IE in JavaScript
|
||||
* without resorting to user-agent sniffing
|
||||
*
|
||||
* @returns {Number|undefined} Number of IE version (5-9), otherwise undefined
|
||||
*/
|
||||
module.exports = function ieVersion() {
|
||||
var undef;
|
||||
var v = 3;
|
||||
var div = document.createElement('div');
|
||||
var all = div.getElementsByTagName('i');
|
||||
|
||||
while ((
|
||||
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
|
||||
all[0]
|
||||
));
|
||||
|
||||
return v > 4 ? v : undef;
|
||||
};
|
||||
@@ -0,0 +1,21 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
An alert should be shown with the <code>{"name":"axios"}</code>
|
||||
|
||||
<script src="promise.js"></script>
|
||||
<script src="../../dist/axios.js"></script>
|
||||
<script>
|
||||
axios.get('./fixture.json').then(function(response) {
|
||||
console.log(response);
|
||||
alert(JSON.stringify(response.data));
|
||||
alert('response headers:\n\n' + JSON.stringify(response.headers));
|
||||
}, function(err) { console.log(err) });
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
An alert should be shown with <code>{"status":"ok"}</code>
|
||||
|
||||
<script src="promise.js"></script>
|
||||
<script src="../../dist/axios.js"></script>
|
||||
<script>
|
||||
axios.get('http://cors-test.appspot.com/test').then(function(response) {
|
||||
alert(JSON.stringify(response.data));
|
||||
alert('response headers:\n\n' + JSON.stringify(response.headers));
|
||||
}, function(err) { console.log(err) });
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"name": "axios"
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user