fetch用post跨域请求数据


postData('https://solelynet.com/public/index.php/api/v1/UserProduct/GetList', {//url  地址和请求参数调用下面postData方法
"is_page":true,
"currentPage":1,
"pagesize":2,
"thirdapp_id":2,}
)
  .then(data => console.log(data)) // JSON from `response.json()` call
  .catch(error => console.error(error))


function postData(url, data) {
  // Default options are marked with *
  return fetch(url, {
    body: JSON.stringify(data), // must match 'Content-Type' header
    cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
    credentials: 'same-origin', // include, same-origin, *omit
    headers: {
      'user-agent': 'Mozilla/4.0 MDN Example',
      'content-type': 'application/json'
    },
    method: 'post', // *GET, POST, PUT, DELETE, etc.
    mode: 'cors', // no-cors, cors, *same-origin//设置是否跨域请求cors跨域no-cors不跨域
    redirect: 'follow', // manual, *follow, error
    referrer: 'no-referrer', // *client, no-referrer
  })
  .then(response => response.json()) // parses response to JSON

}

具体写法请参考fetch  API网址https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

猜你喜欢

转载自blog.csdn.net/weixin_40888956/article/details/79976319