跳至主要內容

fetch

chanchaw小于 1 分钟react

post

function httpReq(){
  const URL = 'http://17.71.0.29:8080/PADEMIS/sw/checkPallet4In'
  // 实例化一个Request实例
  // 第一个参数一般指资源路径
  // 第二个参数可以理解为请求的配置项,包含头部信息和http请求一些关键配置(请求类型、参数...)
  let requestInstance = new Request(URL, {
  method: 'post',
  headers: {
      'Content-Type': 'application/json;charset=utf-8'
  },
  body: '{"palletCode":"A0012"}'
  })
  // fetch方法参数同Request实例
  // 第一个参数为url或者Request实例
  // 第二个参数为请求配置项
  fetch(requestInstance).then(response => {
    // 返回的是一个Response的实例
    // 调用Response实例的序列化方法,序列化成json,返回值是一个promise
    // 序列化方法有 json,text,formData,blob,arrayBuffer,redirct
    let result = response.json()
    result.then(res => {
        console.log(res)
    })
  }).catch(err => console.log(err)).finally(() => console.log('网络请求执行完毕!'))
}