Skip to content

Commit

Permalink
fix: request
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Dec 1, 2018
1 parent 4268fa5 commit 918550b
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ export default function request<T extends RequestOptions>(options: T): Promise<{

// 浏览器请求
if (inBrowser()) {
const isGet = options.method === 'GET'
const queryString = objectToQueryString(options.data)
const url = options.url + (
isGet ? (options.url.indexOf('?') > -1 ? '&' : '?') + queryString : ''
)
let requestBody: string = null
let url = options.url
if (options.requestDataType === 'json') {
requestBody = JSON.stringify(options.data)
} else {
const queryString = objectToQueryString(options.data)
if (options.method === 'GET') {
url += (url.indexOf('?') > -1 ? '&' : '?') + queryString
} else {
requestBody = queryString
}
}
const xhr = new XMLHttpRequest()
xhr.open(options.method, url)
xhr.responseType = options.responseDataType
Expand All @@ -132,7 +139,7 @@ export default function request<T extends RequestOptions>(options: T): Promise<{
xhr.onerror = reject
xhr.ontimeout = reject
xhr.onabort = reject
xhr.send(isGet ? null : queryString)
xhr.send(requestBody)
}
})
}

0 comments on commit 918550b

Please sign in to comment.