vue-resource对于返回的401能做统一的设置处理么?

看了下vue-resource文档的全局属性设置,不是很懂,如果返回401等错误码的时候,怎么全局统一设置显示返回的message?

Vue.http.interceptors.push((request, next) => {
// modify request ...

// stop and return response
next(request.respondWith(body, {

status: 404,
statusText: 'Not found'

}));
});

阅读 5.2k
1 个回答
next((response) => {
    if (response.status === 404) {
        // 以下只是示范,也可以替换成其他的,比如 response.body 替换成其他的 json 对象,status 替换成 200 ,然后在请求里边应该就可以使用 then() 的第一个函数参数定义这个 404 回调了
        request.respondWith(response.body, {
            status: 404,
            statusText: 'Not Found'
        })
    } else {
        // ...
    }
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进