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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 3,215评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,659评论 19 139
  • # Ajax标签(空格分隔): 笔记整理---[TOC]### 从输入网址开始:- 在学习ajax之前,你应该先了...
    V8阅读 344评论 1 0
  • 《宫心计》由佘诗曼、杨怡、陈豪、郑嘉颖领衔主演,梅小青担任监制。该剧主要以唐朝宫廷为背景,讲述了唐朝后宫为权为情尔...
    A虎嗅蔷薇阅读 247评论 0 1
  • 01 晚十点我走出图书馆,本来迈向寝室的腿收了回来,自转30度,最终选择了夜跑的小路。 沿水泥路右边的白线走,到路...
    须川亮阅读 400评论 0 1

友情链接更多精彩内容