Fetch API 提供了一个 JavaScript 接口,用于访问和操纵 HTTP 管道的一些具体部分,例如请求和响应。它还提供了一个全局 fetch() 方法,该方法提供了一种简单,合理的方式来跨网络异步获取资源
const head=new Headers()头部请求
head.append("Content-Type","application/x-www-form-urlencoded")
const req=new Request("http://localhost/tp5/publica/index/Api/index")
// console.log(document.body.clientHeight)
fetch(req,{
method: "POST",//get 请求修改content-type:"application/json"然后通过url附加后缀比起ajax 麻烦一点吧 但是 好用就不错
headers:head,
body:"user=liu"} )//用于post data can be `string` or {object}!})
.then(function(response) {
console.log(response)返回一个promise对象异步的 resovle reject
response.json().then((data)=>{
console.log(data)
})
})
.catch(function(err){
console.log(err)
})