react 数据交互

axios

fetch语法:

    fetch('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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。