Skip to content
This repository has been archived by the owner on Jul 24, 2023. It is now read-only.

Commit

Permalink
feat(request): resolve all HTTP status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
CanRau committed Oct 23, 2020
1 parent e50f7c2 commit de17ab2
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@ const defaultHeaders = {

// from https://ccoenraets.github.io/es6-tutorial-data/promisify/
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest
export const request = obj =>
export const request = (obj) =>
new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.open(obj.method || `GET`, obj.url)
const headers = Object.assign({}, defaultHeaders, obj.headers)
if (headers) {
Object.keys(headers).forEach(key => {
Object.keys(headers).forEach((key) => {
xhr.setRequestHeader(key, headers[key])
})
}
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response)
} else {
reject({ status: xhr.status, message: xhr.statusText })
}
// if (xhr.status >= 200 && xhr.status < 300) {
resolve(xhr.response)
// } else {
// reject({ status: xhr.status, message: xhr.statusText })
// }
}
xhr.onerror = () => reject(xhr.statusText)
xhr.send(obj.body)
Expand Down

0 comments on commit de17ab2

Please sign in to comment.