Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Axios & Fetch API #73

Open
xgqfrms opened this issue Feb 6, 2020 · 1 comment
Open

Axios & Fetch API #73

xgqfrms opened this issue Feb 6, 2020 · 1 comment
Labels
Axios & catch 500 error response data Axios & catch 500 error response data Axios & Fetch API Axios & Fetch API

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Feb 6, 2020

Axios & Fetch API

Axios & catch 500 error response data

const log = console.log;

  updateData(options = {}, flag = false){
    let url = `/opapi/appDownloadGuide/update`;
    let message = '更新成功!';
    let errorMessage = '更新失败!';
    if (flag) {
      message = '添加成功!';
      errorMessage = '添加失败!';
      url = `/opapi/appDownloadGuide/add`;
    }
    axios
    .post(url, options)
    .then(res => {
      // ❓🤔️500 , 在这里拦不住呀?
      log(`res`, res, res.status);
      return res.data;
    })
    .then(json => {
      const {
        code,
        success,
        data,
        errorCode,
        errorHint,
      } = json;
      if(code === 200) {
        this.$message({
          type: 'success',
          message,
        });
        this.init();
      } else{
        this.$message({
          type: 'error',
          message: `${errorMessage}: ${errorHint ? errorHint : ""}`,
        });
      }
    })
    .catch(err => {
      // 👌在这里拦截 error data!
      log(`error.response`, err.response);
      const {
        data,
        status,
        statusText,
      } = err.response;
      this.$message({
        type: "error",
        message: `${statusText}-${status}: ${data || ""}`,
        // message: "不能重复创建",
      });
      console.error(`500 err`, err);
    });
  },

https://stackoverflow.com/questions/38798451/how-to-catch-and-handle-error-response-422-with-redux-axios

@xgqfrms
Copy link
Owner Author

xgqfrms commented Feb 6, 2020

@xgqfrms xgqfrms added Axios & Fetch API Axios & Fetch API Axios & catch 500 error response data Axios & catch 500 error response data labels Feb 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Axios & catch 500 error response data Axios & catch 500 error response data Axios & Fetch API Axios & Fetch API
Projects
None yet
Development

No branches or pull requests

1 participant