fetch-jsonp是github上的开源项目,使用简单:
安装 :npm install fetch-jsonp ;
引用 :import fetchJsonp from 'fetch-jsonp';
详细链接:fetch-jsonp
本篇文章要说明的是一款跨域数据请求的库文件fetch-jsonp,这是一款github上开源文件,基于ES6下的Promise设计实现,可以在某些情况下替代$.ajax实现数据的跨域访问,github上有非常详细的API说明及使用实例。
首先此方法只支持GET方法,而且方法基于ES6的Promise实现:
fetchJsonp('/访问的地址.jsonp')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json); // 在此处进行接收数据之后的操作
}).catch(function(ex) {
console.log('parsing failed', ex) // 此处是数据请求失败后的处理
})
这样就可以实现跨域请求,还支持then方法异步处理,是不是特别简单。
function fetchServerUrl() {
return fetchJsonp(serverUrl, {
timeout: 10000,
})
.then((response) => response.json())
.then((result) => ({
...result,
ip: result.serverIp,
}))
.then((result) => ({ ...result, serverIp: result.ip }));
}
352




被折叠的 条评论
为什么被折叠?



