芯が強い人になるESTJ-A

# 使用fetch-jsonp进行跨域以及参数的传递

IT開発 Tags: 无标签 阅读: 203
fetch-jsonp  不受跨域限制

安装  cnpm i fetch-jsonp -S

用法 : 在项目中引入
import  fetchJsonp  from  fetch-jsonp

fetchJsonp("请求的url地址")
.then(response => res.json() )
.then(data => console.log(data))
.catch(err => console.log(err));
  //注意: 第一个.then 中获取到的不是最终数据,而是一个中间的数据流对象;
  // 注意: 第一个  .then 中获取到的数据, 是一个 Response 类型对象;
  // 注意: 第二个 .then 中,获取到的才是真正的 数据;


案例:
const fetchUrl = `url地址`;
fetchJsonp(fetchUrl)
  .then(res => res.json())
  .then(data => console.log(data))
  .catch(err => console.log(err));

const fetchUrl = `url地址`;
fetchJsonp(fetchUrl)
  .then(res => res.json())
  .then(data => showDatas(data.results))
  .catch(err => console.log(err));

function showDatas(music){
    console.log(music);
}

github:
https://github.com/camsong/fetch-jsonp