dva icon indicating copy to clipboard operation
dva copied to clipboard

request.js中fetch执行后浏览器console抛出异常

Open loccmodd opened this issue 9 years ago • 12 comments

参考antd-admin在开发登录界面,目前点击提交按钮后, 一:调用到了model下的loginModel.js的login方法,loginModel.js 代码如下: import { login } from '../services/login' import { routerRedux } from 'dva/router' import { queryURL } from 'utils' export default { namespace: 'loginModel', state: { loginLoading: false, }, effects: { *login({ payload, }, { put, call }) { yield put({ type: 'showLoginLoading' }) const data = yield call(login, payload) yield put({ type: 'hideLoginLoading' }) if (data.success) { const from = queryURL('from') yield put({ type: 'app/query' }) if (from) { yield put(routerRedux.push(from)) } else { yield put(routerRedux.push('/dashboard')) } } else { throw data } }, }, reducers: { showLoginLoading(state) { return { ...state, loginLoading: true, } }, hideLoginLoading(state) { return { ...state, loginLoading: false, } }, }, }; 二:loginModel.js中的login方法中call到了services下的login.js脚本中的login方法,login.js代码如下: 通过打印输出,url是非空的。 import { request, config } from 'utils' const { api } = config const { userLogin } = api export async function login (data) { return request({ url: userLogin, method: 'get', data, }) } 三:login.js调用了request.js中的request方法,fetch也有执行,下面是request.js代码: import fetch from 'dva/fetch'; function parseJSON(response) { // return response.json(); return null; } function checkStatus(response) { if (response.status >= 200 && response.status < 300) { return response; } const error = new Error(response.statusText); error.response = response; throw error; } export default function request(url, options) { return fetch(url, options) .then(checkStatus) .then(parseJSON) .then(data => ({ data })) .catch(err => ({ err })); } 四,request.js中的fetch执行后,浏览器console中打印出如下错误内容: uncaught at _callee3 at _callee6 at takeEvery(loginModel/login, sagaWithCatch) at sagaWithCatch Error: [object Object] at http://localhost:8000/index.js:12206:16 at http://localhost:8000/index.js:24392:27 at onErrorWrapper (http://localhost:8000/index.js:12211:12) at sagaWithCatch$ (http://localhost:8000/index.js:12570:18) at tryCatch (http://localhost:8000/index.js:12841:41) at Generator.invoke [as _invoke] (http://localhost:8000/index.js:13079:23) at Generator.prototype.(anonymous function) [as throw] (http://localhost:8000/index.js:12893:22) at next (http://localhost:8000/index.js:22415:33) at Object.currCb [as cont] (http://localhost:8000/index.js:22514:8) at end (http://localhost:8000/index.js:22481:24) 同时浏览器network中并未发现有请求发出,截图如下: sp170715_162531

求教大家这怎么解决? @sorrycc

loccmodd avatar Jul 15 '17 08:07 loccmodd

我也遇到了,请问你解决了吗?

wupinlang avatar Jul 18 '17 16:07 wupinlang

我知道了,你的request接受2个参数,你传了一个对象

wupinlang avatar Jul 18 '17 16:07 wupinlang

function request(url, options) 所以你的调用应该是:

request(userLogin, {
   method: 'get',
   data,
})

这样的吧

LeezQ avatar Jul 19 '17 11:07 LeezQ

@sorrycc 请问这个问题怎么解决啊大神

zoushenglin avatar Nov 25 '17 14:11 zoushenglin

@loccmodd 请问你这个问题怎么解决的啊?我也遇到了这样的问题,教教我啊

zoushenglin avatar Nov 25 '17 14:11 zoushenglin

解决了吗,我也遇到了。不知道出错在哪里。

YeeeeeS avatar Dec 06 '17 03:12 YeeeeeS

@LeezQ @wupinlang 即使是参数问题,也不能造成程序crash!

Liqiankun avatar Jan 09 '18 10:01 Liqiankun

@zoushenglin 是不是出现这个报错程序就崩溃了

Liqiankun avatar Jan 09 '18 10:01 Liqiankun

got this problem too. I solved it in the following way: when getting error from request then I return resolved promise with {success: false} instead of rejected one

mpalianytsia avatar Jan 10 '18 10:01 mpalianytsia

怎么解决的?也没个回答了啊···

zhangyang110 avatar Mar 26 '18 07:03 zhangyang110

const app = dva({
  onAction: middlewares,
  onError: (err, dispatch) => {
    if (err.response) {
    }
  }
})

catch the error in index file @zhangyang110

Liqiankun avatar Mar 28 '18 04:03 Liqiankun

request( ${userLogin}${params} )这样就解决了

Tongyh avatar Sep 07 '18 06:09 Tongyh