request.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var stringifyPrimitive = function(v) {
  2. switch (typeof v) {
  3. case 'string':
  4. return v;
  5. case 'boolean':
  6. return v ? 'true' : 'false';
  7. case 'number':
  8. return isFinite(v) ? v : '';
  9. default:
  10. return '';
  11. }
  12. };
  13. var queryStringify = function(obj, sep, eq, name) {
  14. sep = sep || '&';
  15. eq = eq || '=';
  16. if (obj === null) {
  17. obj = undefined;
  18. }
  19. if (typeof obj === 'object') {
  20. return Object.keys(obj).map(function(k) {
  21. var ks = encodeURIComponent(stringifyPrimitive(k)) + eq;
  22. if (Array.isArray(obj[k])) {
  23. return obj[k].map(function(v) {
  24. return ks + encodeURIComponent(stringifyPrimitive(v));
  25. }).join(sep);
  26. } else {
  27. return ks + encodeURIComponent(stringifyPrimitive(obj[k]));
  28. }
  29. }).filter(Boolean).join(sep);
  30. }
  31. if (!name) return '';
  32. return encodeURIComponent(stringifyPrimitive(name)) + eq +
  33. encodeURIComponent(stringifyPrimitive(obj));
  34. };
  35. var xhrRes = function (err, xhr, body) {
  36. var headers = {};
  37. xhr.getAllResponseHeaders().trim().split('\n').forEach(function (item) {
  38. if (item) {
  39. var index = item.indexOf(':');
  40. var key = item.substr(0, index).trim().toLowerCase();
  41. var val = item.substr(index + 1).trim();
  42. headers[key] = val;
  43. }
  44. });
  45. return {
  46. error: err,
  47. statusCode: xhr.status,
  48. statusMessage: xhr.statusText,
  49. headers: headers,
  50. body: body,
  51. };
  52. };
  53. var xhrBody = function (xhr, dataType) {
  54. return !dataType && dataType === 'text' ? xhr.responseText : xhr.response;
  55. };
  56. var request = function (opt, callback) {
  57. // method
  58. var method = (opt.method || 'GET').toUpperCase();
  59. // url、qs
  60. var url = opt.url;
  61. if (opt.qs) {
  62. var qsStr = queryStringify(opt.qs);
  63. if (qsStr) {
  64. url += (url.indexOf('?') === -1 ? '?' : '&') + qsStr;
  65. }
  66. }
  67. // 创建 ajax 实例
  68. var xhr = new XMLHttpRequest();
  69. xhr.open(method, url, true);
  70. xhr.responseType = opt.dataType || 'text';
  71. // 处理 xhrFields 属性
  72. if (opt.xhrFields) {
  73. for (var xhrField in opt.xhrFields) {
  74. xhr[xhrField] = opt.xhrFields[xhrField]
  75. }
  76. }
  77. // 处理 headers
  78. var headers = opt.headers;
  79. if (headers) {
  80. for (var key in headers) {
  81. if (headers.hasOwnProperty(key) &&
  82. key.toLowerCase() !== 'content-length' &&
  83. key.toLowerCase() !== 'user-agent' &&
  84. key.toLowerCase() !== 'origin' &&
  85. key.toLowerCase() !== 'host') {
  86. xhr.setRequestHeader(key, headers[key]);
  87. }
  88. }
  89. }
  90. // onprogress
  91. if (opt.onProgress && xhr.upload) xhr.upload.onprogress = opt.onProgress;
  92. if (opt.onDownloadProgress) xhr.onprogress = opt.onDownloadProgress;
  93. // timeout
  94. if (opt.timeout) xhr.timeout = opt.timeout;
  95. xhr.ontimeout = function(event){
  96. var error = new Error('timeout');
  97. callback(xhrRes(error, xhr));
  98. };
  99. // success 2xx/3xx/4xx
  100. xhr.onload = function () {
  101. callback(xhrRes(null, xhr, xhrBody(xhr, opt.dataType)));
  102. };
  103. // error 5xx/0 (网络错误、跨域报错、Https connect-src 限制的报错时 statusCode 为 0)
  104. xhr.onerror = function (err) {
  105. var body = xhrBody(xhr, opt.dataType);
  106. if (body) { // 5xx
  107. callback(xhrRes(null, xhr, body));
  108. } else { // 0
  109. var error = xhr.statusText;
  110. if (!error && xhr.status === 0) error = new Error('CORS blocked or network error');
  111. callback(xhrRes(error, xhr, body));
  112. }
  113. };
  114. // send
  115. xhr.send(opt.body || '');
  116. // 返回 ajax 实例,用于外部调用 xhr.abort
  117. return xhr;
  118. };
  119. module.exports = request;