ch('url接口地址',
{
配置信息
)
.then(res=>{
return res.json() //將數(shù)據(jù)轉(zhuǎn)換成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跨域方案:http://www.reibang.com/p/95b2caf7e0da
后端跨域:cors,后端代理(例如:反向代理 nginx)
fetch數(shù)據(jù)交互:https://segmentfault.com/a/1190000003810652