zoukankan      html  css  js  c++  java
  • fetch网络请求基本用法

    getRequest(){
    
            fetch("http://127.0.0.1:8888/getReq?naem=张三",{method:"get"}).then(res=>{
                //fech第一个then方法里的参数并不是直接返回的数据,需要对该对象做序列化处理(json),再第一个then里才能拿到数据
                return res.json();
                console.log(res)
            }).then(data=>{
                console.log(data)
            }).catch(err=>{
                console.log(err)
            })
    
        }
    
        postRequest(){
            /*
            * post请求如果传递数据,数据写在第二个参数对象里的body字段里,数据类型可以是json数据格式(json.string()),也可以是querystring格式
            * */
            fetch("http://127.0.0.1:8888/postReq",{
                method:"post",
                // body:JSON.stringify({name:"张三",age:"20"})
                // body:"name=张三"
                body:qs.stringify({name:"张三",age:"20"}),
                // headers:{"Content-Type":"application/json"}
                headers:{"Content-Type":"application/x-www-from-urlencoded"}
            }).then(res=>{
    
                return res.json();
                console.log(res)
            }).then(data=>{
                console.log(data)
            }).catch(err=>{
                console.log(err)
            })
    
    
    勤学似春起之苗,不见其增,日有所长; 辍学如磨刀之石,不见其损,日所有亏!
  • 相关阅读:
    1491: [NOI2007]社交网络
    Ombrophobic Bovines
    1566: [NOI2009]管道取珠
    1564: [NOI2009]二叉查找树
    1497: [NOI2006]最大获利
    mysql数据库修改字段及新增字段脚本
    NIO
    Lombok用法及标签释义
    idea2017.2延长使用时间
    IDEA 注册码
  • 原文地址:https://www.cnblogs.com/qiaozhiming123/p/15029143.html
Copyright © 2011-2022 走看看