react数据交互

2018-07-05  本文已影响0人  Abby_qi

ch('url接口地址',
{
配置信息
)
.then(res=>{
return res.json() //将数据转换成promise可返回的json
})
.then(result=>{
console.log('result',result)
this.state.goods = result.goods
})
.catch(error=>{ //报错时走catch
console.log(error)
})

例如:
fetch('./api/goods.json',
{
credentials: 'include',
method: 'get',
body: JSON.stringify({ user: 'lisposter', pwd: 'souche.com'}
)
.then(res=>{
return res.json()
})
.then(result=>{
console.log('result',result)
this.state.goods = result.goods
})
.catch(error=>{
console.log(error)
})

react跨域:

前端跨域:jsonp,代理

react跨域方案:在package.json中添加

  "proxy": {
    "/v2": {
      "target": "https://api.douban.com",
      "changeOrigin":true
    }
  }

vue跨域方案:https://www.jianshu.com/p/95b2caf7e0da

后端跨域:cors,后端代理(例如:反向代理 nginx)

fetch数据交互:https://segmentfault.com/a/1190000003810652

上一篇 下一篇

猜你喜欢

热点阅读