Skip to content

Commit

Permalink
Merge pull request #1308 from didi/fix-api-proxy-web-datatype-text
Browse files Browse the repository at this point in the history
fix: 修复输出web,mpx.request datatype=text时返回值为json问题
  • Loading branch information
hiyuki authored Oct 26, 2023
2 parents f533e80 + 6dc4871 commit bd6c1af
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/api-proxy/src/web/api/request/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ function request (options = { url: '' }) {
headers: header,
responseType,
timeout,
cancelToken: source.token
cancelToken: source.token,
transitional: {
// silent JSON parsing mode
// `true` - ignore JSON parsing errors and set response.data to null if parsing failed (old behaviour)
// `false` - throw SyntaxError if JSON parsing failed (Note: responseType must be set to 'json')
silentJSONParsing: true, // default value for the current Axios version
// try to parse the response string as JSON even if `responseType` is not 'json'
forcedJSONParsing: false,
// throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts
clarifyTimeoutError: false
}
}

if (method === 'GET') {
Expand All @@ -49,7 +59,7 @@ function request (options = { url: '' }) {

const promise = axios(rOptions).then(res => {
let data = res.data
if (responseType === 'text' && dataType === 'json') {
if (dataType === 'json' && typeof data === 'string') {
try {
data = JSON.parse(data)
} catch (e) {
Expand Down

0 comments on commit bd6c1af

Please sign in to comment.