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
    

      

     

  • 相关阅读:
    aspx有"记住我"的登录
    Aspx比较简单的登录
    内容显示分页数字分页 aspx
    Ashx登录
    Aspx 验证码_各种封装
    IsPostBack的使用
    Ashx增删改查_动软
    一般处理程序ashx
    dispatch_after
    pch文件的作用
  • 原文地址:https://www.cnblogs.com/Miao--miao/p/13895505.html
Copyright © 2011-2022 走看看