欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fetch用法

程序员文章站 2022-04-15 18:38:21
...

fetch(get)

var data = [];
fetch('[url]?data=' + data, {
        method: 'POST',
      }).then(function (res) {
        console.log('res', res)
      }).catch(function (e) {
        console.log('e', e)
      })

fetch(post)

fetch("url",{
  method:'POST',
  headers:{
     'Content-Type': 'application/json'
  },
  body:JSON.stringify({
    name:'john',
    pass:'xioayuan'
  })
})
.then((response)=>response.json())
.then((responseJsonData)=>{
  alert("请求成功");
  console.log(responseJsonData);
})
.catch((error)=>{
  alert(error);
})

fetch(post 表单)

login.onclick=function (){
	var formdata=new FormData([表单]);
fetch("/lf1/L",
	{method:'post',
	 
	body:encodeURI(Array.from(formdata.entries()).map(el=>el.join('=')).join('&')),
	headers:{
     	"Content-type": "application/x-www-form-urlencoded; charset=utf-8"
     	}
    
     }
     	
	).then(function (rrr) {
		return rrr.json();
	})
	.then(function (aaa) {
     if(aaa.status==2){
            document.getElementById("error_login").style.display="block";
		}
	  if(aaa.status==1){
	  		sessionStorage.setItem('user',JSON.stringify(aaa.data));
            window.location.href='lin.html';
		}	
		
	});

}	

fetch (复杂传参)

function fabu() {
	   var thought=document.getElementById("thought").value;
	   var user=sessionStorage.getItem('user');
	   var user1=JSON.parse(user);
       var pid = product.pid;

        var s={};
        s["pid"]=pid;
        s["uid"]=user1.uid;
        s["content"]=thought
        fetch('/lf1/Cp', {
            method: 'POST',
            body:Object.keys(s).map(el=>`${el}=${s[el]}`).join('&'),

            headers: {
                "Content-type": "application/x-www-form-urlencoded; charset=utf-8"
            }
        })
            .then(function (rrr) {
            	console.log(rrr);
                return rrr.json();
            }).then(function (aaa) {
         comment_pers=aaa;
            });

    }
相关标签: fetch

上一篇: fetch数据请求

下一篇: fetch