如何显示响应ajax的错误?

时间:2018-03-17 01:06:21

标签: javascript jquery ajax vue.js vue-component

我的脚本ajax是这样的:

this.$store.dispatch('changePassword', {password})
.then((response) => {
    console.log('resp')
    console.log(response)
})
.catch(error => {
    console.log('er')
    console.log(error)
});

我使用vue component / vuex store

当脚本运行时,选项卡控制台上的结果如下:

enter image description here

如果我在标签网络中检查标签响应,结果如下:

enter image description here

我想获得价值:

  

旧密码确认不匹配。

我有响应/错误的console.log,但我找不到对象

我如何获得价值?

更新

我的vuex模块如下:

import { set } from 'vue'
import users from '../../api/users'
import * as types from '../mutation-types'
// initial state
const state = {
    addStatusChangePassword: null
}
// getters
const getters = {
    changePasswordStatus: state =>  state.changePasswordStatus,
}
// actions
const actions = {
    changePassword ({ dispatch,commit,state },{password})
    {
        return users.changePassword(password,
            data => {
                commit(types.CHANGE_PASSWORD_SUCCESS)
            },
            errors => {
                commit(types.CHANGE_PASSWORD_FAILURE)
            }
        )
    }
}
// mutations
const mutations = {
    [types.CHANGE_PASSWORD_SUCCESS] (state){
        state.addStatusChangePassword = 'success'
    },
    [types.CHANGE_PASSWORD_FAILURE] (state){
        state.addStatusChangePassword = 'failure'
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}

我的vuex api是这样的:

import Vue from 'vue'
export default {
    // api to change password
    changePassword (password, cb, ecb = null) {
        return axios.post('/member/profile/change-password', password).then(
            (resp) => {
                return resp.data;
            }, 
            (resp) => {
                return resp.data;
            }
        );
    }
}

1 个答案:

答案 0 :(得分:0)

changePassword data你正在通过你的changePassword,但你是:

  • 不区分错误(data的两条路径只返回数据);也是
  • 忽略行动中的errorsPromise参数。

以下更改。

changePassword中添加适当的import Vue from 'vue' export default { // api to change password changePassword (password, cb, ecb = null) { return new Promise((resolve, reject) => // added axios.post('/member/profile/change-password', password).then( (resp) => { resolve(resp.data); // resolve() }, (resp) => { reject(resp); // reject() without .data } ) ); } }

Promise

并且,在您的操作中,还要添加// actions const actions = { changePassword ({ dispatch,commit,state },{password}) { return new Promise((resolve, reject) => // added users.changePassword(password, data => { commit(types.CHANGE_PASSWORD_SUCCESS) resolve(data); // forward }, errors => { commit(types.CHANGE_PASSWORD_FAILURE) reject(errors); // forward } )); } }

fill