用axios请求node做的简单网络接口教学易懂

简介: 用axios请求node做的简单网络接口教学易懂

非常简单直接看代码,不懂的直接扣我

前端页面

<script>
  import axios from 'axios'
  // console.log(axios.prototype);
export default {
  name:'AxiosBase',
  data() {
    return {
      list:''
    }
  },
  methods: {
    getData(){
      // 发送基本请求
      // axios.get('http://localhost:4000/data')
      // .then((res)=>{
      //   console.log(res);
      // })
      // .catch((err)=>{
      // })
      // axios默认get请求
      // axios('http://localhost:4000/data')
      // .then((res)=>{
      //   console.log(res);
      // })
      // .catch((err)=>{
      // })
      // 指定get请求的参数
      // axios('http://localhost:4000/data?id=1')
      // .then((res)=>{
      //   console.log(res);
      // })
      // .catch((err)=>{
      // })
      // 这里加的这个id没有用的需要后端提供接口
      axios('http://localhost:4000/data',{
        // 指定get请求的参数,进行拼接
        params:{
          id:1
        }
      })
      .then((res)=>{
        // console.log(res);
        this.list = res.data
      })
      .catch((err)=>{
      })
    },
    postData(){
      // 后台是以Form Data方式处理数据的
      // 不用这个URLSearchParams,post后台收不到这个数据,处理方式有很多种
      var stu = new URLSearchParams()
      // 设置值    键名    值
      stu.append('name','123'),
      stu.append('sex','男')
      axios.post('http://localhost:4000/user',stu)
      .then((res)=>{
        console.log(res);
      })
      .catch((err)=>{
      })
    }
  },
}
</script>

后端是node的express框架

1.// 引入express
const express = require('express');
//这个组件用于接收post数据
const bodyParser = require('body-parser');
// 创建服务器对象
const app = express();
// 注册中间件
app.use(bodyParser.urlencoded({extended:false}))
app.use(bodyParser.json({type:'application/*+json'}))
//本地服务器解决跨域,不可缺
app.all('*',function(req,res,next){
  res.header('Access-Control-Allow-Origin','*')
  res.header('Access-Control-Allow-Headers','Content-Type')
  res.header('Access-Control-Allow-Methods','*');
  res.header('Content-Type','application/json;charset=utf-8');
  next();
})
// 处理get请求
app.get('/data',(req,res)=>{
  // 设置请求头,解决跨域,上面已经都解决了
  // res.setHeader('Access-Control-Allow-Origin','*');
  res.json({
    todos:[
      {
        "id":1,
        "name":"chen",
        "age":20,
        "sex":"男"
      },
      {
        "id":2,
        "name":"xin",
        "age":10,
        "sex":"女"
      },
    ]
  })
})
// req后台向前台请求
// res后台向前台响应
app.post('/user',(req,res)=>{
  res.json({
    user:req.body,//前端传什么数据,后台就传什么数据给你
    msg:'post请求成功',
  })
  console.log('接收',req.body);
})
//开启监听
app.listen(4000,()=>{
  console.log('4000端口已经启动。。。');
})
相关文章
|
2月前
|
前端开发 小程序 Java
uniapp-网络数据请求全教程
这篇文档介绍了如何在uni-app项目中使用第三方包发起网络请求
109 3
|
2月前
|
存储 监控 算法
局域网网络管控里 Node.js 红黑树算法的绝妙运用
在数字化办公中,局域网网络管控至关重要。红黑树作为一种自平衡二叉搜索树,凭借其高效的数据管理和平衡机制,在局域网设备状态管理中大放异彩。通过Node.js实现红黑树算法,可快速插入、查找和更新设备信息(如IP地址、带宽等),确保网络管理员实时监控和优化网络资源,提升局域网的稳定性和安全性。未来,随着技术融合,红黑树将在网络管控中持续进化,助力构建高效、安全的局域网络生态。
65 9
|
6月前
|
缓存 网络协议 CDN
在网页请求到显示的过程中,如何优化网络通信速度?
在网页请求到显示的过程中,如何优化网络通信速度?
220 59
|
6月前
|
JavaScript 前端开发 开发者
vue中使用axios请求post接口,请求会发送两次
vue中使用axios请求post接口,请求会发送两次
|
5月前
|
JavaScript
Node.js GET/POST请求
10月更文挑战第6天
57 2
Node.js GET/POST请求
|
5月前
|
SQL JavaScript 关系型数据库
node博客小项目:接口开发、连接mysql数据库
【10月更文挑战第14天】node博客小项目:接口开发、连接mysql数据库
|
5月前
|
弹性计算 Kubernetes 网络协议
阿里云弹性网络接口技术的容器网络基础教程
阿里云弹性网络接口技术的容器网络基础教程
阿里云弹性网络接口技术的容器网络基础教程
|
6月前
|
数据采集 Web App开发 开发工具
|
6月前
|
数据安全/隐私保护
|
5月前
|
存储 缓存 Ubuntu
配置网络接口的“IP”命令10个
【10月更文挑战第18天】配置网络接口的“IP”命令10个
146 0

热门文章

最新文章