zoukankan      html  css  js  c++  java
  • Express和get,post请求

    1、axios中的get请求:

          (1)请求方式:

               

      axios
            .get(
              'http://localhost:3000/data/sub?' +
                'input=' +
                this.input +
                '&password=' +
                this.password
            )
            .then((res) => {
              console.log(res.data)
            })
            .catch((err) => {
              console.log(err.data)
            })
    

      通过这种直接把传输的内容拼接在连接之后其实是非常不规范的,但是效果是可以实现的,因为第一次接触前后端开发,看到后端写的接口连接地址格式如上,因此第一反应是通过字符串的拼接拼出接口的链接格式。

    2、这种写法应该是比较规范的,看起来也好看了一些

     axios
            .get(
              'http://localhost:3000/data/sub?',{
                params:{
                  input:this.input,
                  params:this.password
                }
              }
            )
            .then((res) => {
              console.log(res.data)
            })
            .catch((err) => {
              console.log(err.data)
            })
    

     通过上面两个方法提交的请求,在Express搭建后端获取到的实际访问链接都是

    GET /data/sub?input=123&password=123 
    

      对于get方法,Express搭建的后端如果想访问前端传递的数据,通过:

    req.query
    

      post发送请求:

    axios
            .post(
              'http://localhost:3000/data/sub?',{
                  input:this.input,
                  params:this.password
              }
            )
            .then((res) => {
              console.log(res.data)
            })
            .catch((err) => {
              console.log(err.data)
            })
    

      Express获取到的链接:

    POST /data/sub?
    

      通过req.body可以获取到传递的数据

    req.body
    

      

     

  • 相关阅读:
    qt 学习(三)消息基础
    qt学习(二)控件
    sqlserver学习_01
    java对文件操作--01
    js 将json字符串转换为json对象的方法解析
    实现动态代理(Java和spring)
    mysql_01_游标的使用
    java实现多文件上传01
    oracle-2_dblink的创建和使用
    sqlserver学习3---sql函数
  • 原文地址:https://www.cnblogs.com/Miao--miao/p/13895505.html
Copyright © 2011-2022 走看看