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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值