Home Web Front-end JS Tutorial How to deal with axios errors

How to deal with axios errors

Mar 28, 2018 pm 02:21 PM
axios deal with mistake

This time I will show you how to deal with axios errors and what are the precautions for dealing with axios errors. The following is a practical case, let's take a look.

Now the official package of vue no longer updates vue-resource, and instead recommends axios. The following is the axios plug-in setting summarized in the actual project:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

/**

 * @file Axios的Vue插件(添加全局请求/响应拦截器)

 */

// https://github.com/mzabriskie/axios

import axios from 'axios'

// 拦截request,设置全局请求为ajax请求

axios.interceptors.request.use((config) => {

 config.headers['X-Requested-With'] = 'XMLHttpRequest'

 return config

})

// 拦截响应response,并做一些错误处理

axios.interceptors.response.use((response) => {

 const data = response.data

// 根据返回的code值来做不同的处理(和后端约定)

 switch (data.code) {

 case '0':

  // 举例

  // exp: 修复iPhone 6+ 微信点击返回出现页面空白的问题

  if (isIOS()) {

  // 异步以保证数据已渲染到页面上

  setTimeout(() => {

   // 通过滚动强制浏览器进行页面重绘

   document.body.scrollTop += 1

  }, 0)

  }

  // 这一步保证数据返回,如果没有return则会走接下来的代码,不是未登录就是报错

  return data

 // 需要重新登录

 case 'SHIRO_E5001':

  // 微信生产环境下授权登录

  if (isWeChat() && IS_PRODUCTION) {

  axios.get(apis.common.wechat.authorizeUrl).then(({ result }) => {

   location.replace(global.decodeURIComponent(result))

  })

  else {

  // 否则跳转到h5登录并带上跳转路由

  const search = encodeSearchParams({

   next: location.href,

  })

  location.replace(`/user/login?${search}`)

  }

  // 不显示提示消息

  data.description = ''

  break

 default:

 }

 // 若不是正确的返回code,且已经登录,就抛出错误

 const err = new Error(data.description)

 err.data = data

 err.response = response

 throw err

}, (err) => { // 这里是返回状态码不为200时候的错误处理

 if (err && err.response) {

 switch (err.response.status) {

  case 400:

  err.message = '请求错误'

  break

  case 401:

  err.message = '未授权,请登录'

  break

  case 403:

  err.message = '拒绝访问'

  break

  case 404:

  err.message = `请求地址出错: ${err.response.config.url}`

  break

  case 408:

  err.message = '请求超时'

  break

  case 500:

  err.message = '服务器内部错误'

  break

  case 501:

  err.message = '服务未实现'

  break

  case 502:

  err.message = '网关错误'

  break

  case 503:

  err.message = '服务不可用'

  break

  case 504:

  err.message = '网关超时'

  break

  case 505:

  err.message = 'HTTP版本不受支持'

  break

  default:

 }

 }

 return Promise.reject(err)

})

axios.install = (Vue) => {

 Vue.prototype.$axios = axios

}

export default axios

Copy after login
I believe you have mastered it after reading the case in this article. For more exciting methods, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How the vue project is released through Baidu's BAE

@HostBinding() and @HostListener in AngularJS ()What is the difference

Why axios http request cannot be used in vue2

The above is the detailed content of How to deal with axios errors. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Unable to complete operation (Error 0x0000771) Printer error Unable to complete operation (Error 0x0000771) Printer error Mar 16, 2024 pm 03:50 PM

If you encounter an error message when using your printer, such as the operation could not be completed (error 0x00000771), it may be because the printer has been disconnected. In this case, you can solve the problem through the following methods. In this article, we will discuss how to fix this issue on Windows 11/10 PC. The entire error message says: The operation could not be completed (error 0x0000771). The specified printer has been deleted. Fix 0x00000771 Printer Error on Windows PC To fix Printer Error the operation could not be completed (Error 0x0000771), the specified printer has been deleted on Windows 11/10 PC, follow this solution: Restart Print Spool

Revealing the causes of HTTP status code 460 Revealing the causes of HTTP status code 460 Feb 19, 2024 pm 08:30 PM

Decrypting HTTP status code 460: Why does this error occur? Introduction: In daily network use, we often encounter various error prompts, including HTTP status codes. These status codes are a mechanism defined by the HTTP protocol to indicate the processing of a request. Among these status codes, there is a relatively rare error code, namely 460. This article will delve into this error code and explain why this error occurs. Definition of HTTP status code 460: First, we need to understand the basics of HTTP status code

Solution to Windows Update prompt Error 0x8024401c error Solution to Windows Update prompt Error 0x8024401c error Jun 08, 2024 pm 12:18 PM

Table of Contents Solution 1 Solution 21. Delete the temporary files of Windows update 2. Repair damaged system files 3. View and modify registry entries 4. Turn off the network card IPv6 5. Run the WindowsUpdateTroubleshooter tool to repair 6. Turn off the firewall and other related anti-virus software. 7. Close the WidowsUpdate service. Solution 3 Solution 4 "0x8024401c" error occurs during Windows update on Huawei computers Symptom Problem Cause Solution Still not solved? Recently, the web server needs to be updated due to system vulnerabilities. After logging in to the server, the update prompts error code 0x8024401c. Solution 1

The operation process of WIN10 service host occupying too much CPU The operation process of WIN10 service host occupying too much CPU Mar 27, 2024 pm 02:41 PM

1. First, we right-click the blank space of the taskbar and select the [Task Manager] option, or right-click the start logo, and then select the [Task Manager] option. 2. In the opened Task Manager interface, we click the [Services] tab on the far right. 3. In the opened [Service] tab, click the [Open Service] option below. 4. In the [Services] window that opens, right-click the [InternetConnectionSharing(ICS)] service, and then select the [Properties] option. 5. In the properties window that opens, change [Open with] to [Disabled], click [Apply] and then click [OK]. 6. Click the start logo, then click the shutdown button, select [Restart], and complete the computer restart.

Interpreting Oracle error 3114: causes and solutions Interpreting Oracle error 3114: causes and solutions Mar 08, 2024 pm 03:42 PM

Title: Analysis of Oracle Error 3114: Causes and Solutions When using Oracle database, you often encounter various error codes, among which error 3114 is a relatively common one. This error generally involves database link problems, which may cause exceptions when accessing the database. This article will interpret Oracle error 3114, discuss its causes, and give specific methods to solve the error and related code examples. 1. Definition of error 3114 Oracle error 3114 pass

Why does the Xiangxiangfuzhai app display an error? Why does the Xiangxiangfuzhai app display an error? Mar 19, 2024 am 08:04 AM

The display error is a problem that may occur in the Xiangxiang Fuzhai app. Some users are not sure why the Xiangxiang Fuzhai app displays errors. It may be due to network connection problems, too many background programs, incorrect registration information, etc. Next, This is the editor’s introduction to how to solve app display errors for users. Interested users should come and take a look! Why does the Xiangxiang Fuzhai app display an error answer: network connection problem, too many background programs, incorrect registration information, etc. Details: 1. [Network problem] Solution: Check the device connection network status, reconnect or choose another network connection to use. Can. 2. [Too many background programs] Solution: Close other running programs and release the system, which can speed up the running of the software. 3. [Incorrect registration information

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

Linux Oops: Detailed explanation of what this error means Linux Oops: Detailed explanation of what this error means Mar 21, 2024 am 09:06 AM

LinuxOops: Detailed explanation of the meaning of this error, need specific code examples What is LinuxOops? In Linux systems, "Oops" refers to a situation where a serious error in the kernel causes the system to crash. Oops is actually a kernel crash mechanism that stops the system when a fatal error occurs and prints out relevant error information so that developers can diagnose and fix the problem. Oops usually occur in kernel space and have nothing to do with user space applications. When the kernel encounters

See all articles