后端


1. URL 查询参数与 axios 使用

什么是查询参数

属性说明
作用在 URL 中传递额外的参数信息
格式?key1=value1&key2=value2
示例http://hmajax.itheima.net/api/province?name=北京&page=1

axios 基本使用

安装 axios

bash
npm install axios

引入 axios

javascript
import axios from 'axios'

发起 GET 请求

javascript
// 方法1:直接在 URL 中添加查询参数
axios.get('http://hmajax.itheima.net/api/province?name=北京')
  .then(response => {
    console.log(response.data)
  })
  .catch(error => {
    console.error(error)
  })

// 方法2:使用 params 选项
axios.get('http://hmajax.itheima.net/api/province', {
  params: {
    name: '北京',
    page: 1
  }
})
  .then(response => {
    console.log(response.data)
  })
  .catch(error => {
    console.error(error)
  })

发起 POST 请求

javascript
axios.post('http://hmajax.itheima.net/api/login', {
  username: 'admin',
  password: '123456'
})
  .then(response => {
    console.log(response.data)
  })
  .catch(error => {
    console.error(error)
  })

2. HTTP 请求方法与 axios 配置

支持的请求方法

方法语法说明
GETaxios.get(url, config)获取资源
POSTaxios.post(url, data, config)提交数据
PUTaxios.put(url, data, config)更新资源
DELETEaxios.delete(url, config)删除资源
PATCHaxios.patch(url, data, config)部分更新资源
HEADaxios.head(url, config)获取响应头
OPTIONSaxios.options(url, config)预检请求

配置选项

选项类型说明
baseURLstring基础URL,会自动拼接到请求URL前
headersobject请求头配置
paramsobject查询参数
dataobject请求体数据
timeoutnumber请求超时时间(毫秒)
withCredentialsboolean是否携带Cookie
authobject认证信息
responseTypestring响应类型,如 'json', 'blob'

全局配置

javascript
// 设置全局配置
axios.defaults.baseURL = 'http://hmajax.itheima.net'
axios.defaults.timeout = 10000
axios.defaults.headers.common['Authorization'] = 'Bearer token'
axios.defaults.headers.post['Content-Type'] = 'application/json'

创建实例

javascript
// 创建 axios 实例
const instance = axios.create({
  baseURL: 'http://hmajax.itheima.net',
  timeout: 10000,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 使用实例发起请求
instance.get('/api/province')
  .then(response => {
    console.log(response.data)
  })
  .catch(error => {
    console.error(error)
  })

3. axios 错误处理

错误类型

类型说明示例
网络错误网络连接问题断网、服务器无响应
超时错误请求超时请求超过设定的timeout时间
4xx错误客户端错误400、401、404等
5xx错误服务器错误500、502、503等

错误处理方法

基本错误处理

javascript
axios.get('/api/province')
  .then(response => {
    console.log(response.data)
  })
  .catch(error => {
    // 处理错误
    if (error.response) {
      // 服务器返回错误状态码
      console.error('状态码:', error.response.status)
      console.error('响应数据:', error.response.data)
    } else if (error.request) {
      // 请求已发出,但没有收到响应
      console.error('没有收到响应:', error.request)
    } else {
      // 请求配置出错
      console.error('请求配置错误:', error.message)
    }
  })

拦截器错误处理

javascript
// 请求拦截器
axios.interceptors.request.use(
  config => {
    // 在发送请求前做些什么
    return config
  },
  error => {
    // 处理请求错误
    return Promise.reject(error)
  }
)

// 响应拦截器
axios.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    return response
  },
  error => {
    // 处理响应错误
    if (error.response) {
      // 服务器返回错误状态码
      switch (error.response.status) {
        case 401:
          // 未授权,跳转到登录页
          window.location.href = '/login'
          break
        case 404:
          // 资源不存在
          console.error('请求的资源不存在')
          break
        case 500:
          // 服务器内部错误
          console.error('服务器内部错误')
          break
        default:
          console.error('请求失败:', error.response.data)
      }
    } else if (error.request) {
      // 请求已发出,但没有收到响应
      console.error('网络错误,请检查网络连接')
    } else {
      // 请求配置出错
      console.error('请求配置错误:', error.message)
    }
    return Promise.reject(error)
  }
)

错误处理最佳实践

  1. 统一错误处理:使用响应拦截器统一处理错误
  2. 友好的错误提示:根据错误类型显示不同的提示信息
  3. 重试机制:对于网络错误,可以实现自动重试
  4. 日志记录:记录错误信息,便于排查问题
  5. 用户体验:在请求过程中显示加载状态,错误时显示友好提示

示例:完整的错误处理

javascript
// 创建 axios 实例
const api = axios.create({
  baseURL: 'http://hmajax.itheima.net',
  timeout: 10000
})

// 响应拦截器
api.interceptors.response.use(
  response => {
    // 对响应数据做点什么
    return response
  },
  error => {
    // 处理响应错误
    let errorMessage = '请求失败'
    
    if (error.response) {
      // 服务器返回错误状态码
      switch (error.response.status) {
        case 400:
          errorMessage = '请求参数错误'
          break
        case 401:
          errorMessage = '未授权,请重新登录'
          // 跳转到登录页
          // window.location.href = '/login'
          break
        case 403:
          errorMessage = '禁止访问'
          break
        case 404:
          errorMessage = '请求的资源不存在'
          break
        case 500:
          errorMessage = '服务器内部错误'
          break
        case 502:
          errorMessage = '网关错误'
          break
        case 503:
          errorMessage = '服务不可用'
          break
        default:
          errorMessage = error.response.data.message || '请求失败'
      }
    } else if (error.request) {
      // 请求已发出,但没有收到响应
      errorMessage = '网络错误,请检查网络连接'
    } else {
      // 请求配置出错
      errorMessage = error.message
    }
    
    // 显示错误提示
    console.error(errorMessage)
    // 可以在这里调用 UI 库的提示组件,如 ElMessage.error(errorMessage)
    
    return Promise.reject(error)
  }
)

// 使用 api 实例发起请求
api.get('/api/province')
  .then(response => {
    console.log('请求成功:', response.data)
  })
  .catch(error => {
    // 这里可以处理特殊的错误情况
    console.error('请求失败:', error)
  })