fetch(url) .then((response) => response.text()) .then((responseText) => { //回调函数,返回数据消息 successCallback(JSON.parse(responseText)); }) .catch(function (err) { failCallback(err); });
getOpenId(code) { this.progressHUD.show(); let requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + wxAppId + "&secret=" + wxAppSecret + "&code=" + code + "&grant_type=authorization_code"; fetch(requestUrl).then((response) => response.json()).then((json) => { console.log('获取微信openid成功'); console.log(json); this.getUnionId(json.access_token, json.openid); }).catch((error) => { this.progressHUD.hide(); this.showAlert('微信授权登录失败'); }) }
/** * Lookup the latitude and longitude for a given address. * @param {string} address Any address that Google understands. * For example: 'Central Park, NYC' works fine. * @return {Promise<Object>} Fetch promise object. */ export default function geocode(address) { return fetch(URL + address).then(res => res.json()); }
export default async function fetchLocal (input: string, init?: Object): Object { try { let result = await fetch(input, init); let resultJson = await result.json(); if (!resultJson.success) { Alert.alert('ImServer Error', resultJson.data.message); } return resultJson; } catch (e) { Alert.alert('Fetch Error', e); } }
const fetchData = (endpoint, fetchAction) => async (dispatch) => { try { dispatch(fetchAction.request()); const response = await fetch(endpoint); const responseJson = await response.json(); dispatch(fetchAction.success(responseJson.items)); } catch (error) { dispatch(fetchAction.failure(error)); } }
const getDistance = async (origin, destination, GOOGLE_MAP_API_KEY) => { const response = await fetch( 'https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins='+ origin.latitude + ',' + origin.longitude + '&destinations=' + destination.latitude + ',' + destination.longitude + '&key=' + GOOGLE_MAP_API_KEY, {method: 'GET'} ); const res = await response.json(); return [res.rows[0].elements[0].distance.value, res.rows[0].elements[0].duration.value]; }
const sendTokenToServer = (token) => { console.log(`Sending token ${token} to server`); fetch(`${Config.SERVER_URL}/token/`, { method: 'POST', headers: { 'Content-Type': 'text/plain', }, body: token, }) .then(() => { console.log('Successfully sent token'); }) .catch((err) => { console.error('Failed to send token', err); }); }
const getHotelListBySearch = search => fetch(`${baseUrl.API_URL}/hotels/search/${search}`, { method: 'GET', headers: { Accept: 'application/json', }, })
export function login() { return dispatch => { dispatch(isLoginOnGoing()); fetch('https://www.baidu.com').then((res) => { dispatch(loginSuccess(user)); }).catch((e) => { dispatch(loginFail("用户名或密码错误")); }) } }
fetchData() { fetch(REQUEST_URL).then((response) => { return response.json() }).then((json) => { this.setState({ dataSource: this.state.dataSource.cloneWithRows(json.subjects), loaded: true, }) }); }
async fetchData(){ try{ let response = await fetch("http://swapi.co/api/people"); let responseJson = await response.json(); this.setState({dataSource: this.state.dataSource.cloneWithRows(responseJson.results)}); //this.props.navigation.setParams({isAnimating: false}); } catch(error){ console.error(error); } }
getUnionId(accessToken, openId) { let requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId; fetch(requestUrl).then((response) => response.json()).then((json) => { console.log('获取微信unionid成功'); console.log(json); // TODO: 这里openId和unionId都已经成功获取了,调用用户自己的接口传递openId或unionId登录或注册 // put your login method here... }).catch((error) => { this.progressHUD.hide(); this.showAlert('微信授权登录失败'); }) }
function* fetchNews() { const json = yield fetch('https://randomuser.me/api/').then(response => response.json()); yield delay(1000); yield put({ type: "NEWS_RECEIVED", json: json.results, }); }
async searchMovies(title) { const query = await fetch(`${BASE_API}/list_movies.json?query_term=${title}&sort_by=rating`); const { data } = await query.json(); return data.movies; }
static all() { return fetch('https://facebook.github.io/react-native/movies.json') .then((response)=>{ return response.json(); }) }