const axios = require('axios'); try { // 发送请求 const response = await axios.get('http://example.com/api/resource'); // 处理响应数据 console.log(response.data); } catch (error) { // 捕获错误 if (error.response && error.response.status === 404) { // 处理404错误 console...
我们可以通过error.response来获取到返回的响应数据,包括状态码等信息。 .catch(error => { console.error(error.response); }); 1. 2. 3. 步骤3:如果状态码为404,则提示用户请求的资源不存在 根据返回的状态码判断是否为404,并给用户相应的提示: .catch(error => { if (error.response.status === 404)...
当我们使用axios发送GET请求读取本地模拟数据时,有时候会遇到报错404的情况。通常的代码示例如下: axios.get('/api/data.json').then(response=>{console.log(response.data);}).catch(error=>{console.log(error);}); 1. 2. 3. 4. 5. 6. 7. 上述代码中,我们期望通过GET请求获取本地的data.json文件,...
console.log(a) // 放在try里 try { // a不打印 console.log(a) }catch(e){ // e是错误信息...
你也可以使用 catch() 要转换错误,只需确保之后重新抛出错误。let error;try { await axios.get('https://httpbin.org/status/404').catch(err => { if (err.response.status === 404) { throw new Error(`${err.config.url} not found`); } throw err; });} catch (err) { ...
.catch((errors) => { console.log(errors); router.push("/"); }); 路由在我的后端定义下 const myEndpointRoute = require('../api/myEndpoint.js')(); exprApp.use('/api/myEndpoint', myEndpointRoute); 作为参考,uri是'http://localhost:3000/api/myEndpoint'。我可以在浏览器中很好地访问这个...
Axios.delete("/",{index:name}) .then((response)=>{console.log(response)}) .catch((err)=>{console.log(err)}); *put是相同的,有另一个数据每当我部署这些代码时,我的控制台中都会出现404错误,而服务器无法获得请求。发布于 4 月前 ✅ 最佳回答: 您没有使用使用正确的baseURL创建的axios实例。
console.log('catch', err); }); } } 模拟Axios: 有两种简单的方法可以模拟 axios,这样您的测试就不会执行真正的 http 请求,而是使用模拟对象: 将axios 设置为组件属性: import axios from 'axios`; Vue.component({ data() { return { axios, ...
(); } else { this.$message({ type: "error", message: "删除失败!!", }); } } else { this.$message({ type: "error", message: res.data.message, }); } }) .catch((err) => {}); }) .catch((_) => { this.$message({ type: "info", message: "已取消删除操作", }); }...
get('/api/book/1220562').then(res => { console.log(res)}).catch(err...