fetch请求

GET请求写法:

	fetch("url路径").then(function(res){
		console.log("成功",res)
	}).catch(function(err){
		console.log("失败",err)
	})

POST请求写法:

    fetch("url路径",{
    	method:"POST",
    	body:JSON.stringify({
    		name:"xxx",
    		age:108
    	}),
    	headers:{
    		"Content-type":"application/json ; charset=UTF-8"
    	}
    }).then(res=>{
    	console.log("成功",res)
    }).catch(err=>{
    	console.log("失败",err)
    })

总结:
和ajax的XMLHttpRequest比起来简洁一些,而且基于Promise,返回的结果也是一个promise

你可能感兴趣的:(HTTP类)