MCPcopy
hub / github.com/dunwu/nginx-tutorial / _doFetch

Function _doFetch

examples/reactapp/src/common/apiutils/reqwestAJAX.js:13–42  ·  view source on GitHub ↗
(options = {}, dispatch, defaultMergeOption = {})

Source from the content-addressed store, hash-verified

11} from './ajaxCommon'
12
13function _doFetch(options = {}, dispatch, defaultMergeOption = {}) {
14 const opts = _.merge({}, defaultMergeOption, options, {
15 url: getRealUrl(options.url),
16 error: wrapErrorHandler(options.onError, dispatch)
17 })
18
19 const method = opts.method && opts.method.toUpperCase()
20 const data = opts.data
21 if (METHODS.GET === method && opts.processData !== false && !_.isString(data)) {
22 // get 请求,配置 processData 不为否,data 不为 String 则预处理
23 const newData = { ...data, ts: new Date().getTime() } // 加入时间戳,防止浏览器缓存
24 opts.data = reqwest.toQueryString(newData, true) // traditional 方式,保证数组符合 spring mvc 的传参方式。
25 }
26
27 opts.success = (res) => {
28 const doSuc = options.onSuccess ? options.onSuccess : defaultOptions.onSuccess // reqwest 名字不同
29 if (opts.type === REQ_TYPE.JSON || typeof res === 'object') {
30 // noinspection JSUnresolvedVariable
31 if (res.result === 0 || res.ret === 0) {
32 doSuc(res.data)
33 } else {
34 opts.error(res)
35 }
36 } else {
37 doSuc(res)
38 }
39 }
40
41 reqwest(opts)
42}
43
44export function doFetch(options = {}, dispatch) {
45 _doFetch(options, dispatch, defaultOptions)

Callers 3

doFetchFunction · 0.85
doFetchJsonFunction · 0.85
doBiJsonFetchFunction · 0.85

Calls 1

getRealUrlFunction · 0.90

Tested by

no test coverage detected