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
    

      

     

  • 相关阅读:
    网页打开微信链接 无法返回
    wap尝试调取app(网易新闻为例)
    兼容性
    图片旋转效果
    a标签发送邮件
    windows 下的bash 环境安装npm
    css 设置滚动条的样式
    前端h5遇到的问题及解决办法
    safari input默认样式
    delphi 新版内存表 FDMemTable
  • 原文地址:https://www.cnblogs.com/Miao--miao/p/13895505.html
Copyright © 2011-2022 走看看