ReactNative 请求网络数据工具类。
/** * Created by chenjialin on 17/3/29. */ 'use strict'; import React, {Component} from 'react' class NetUtils extends React.Component { //post请求 static postFrom(url, data, callback) { var fetchOptions = { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded' }, body:'data='+data+'' }; fetch(url, fetchOptions) .then((response) => response.text()) .then((responseText) => { callback(JSON.parse(responseText)); }).done(); } // static postJson (url, data, callback) { var fetchOptions = { method: 'POST', headers: { 'Accept': 'application/json', //json形式 'Content-Type': 'application/json' }, body: JSON.stringify(data) }; fetch(url, fetchOptions) .then((response) => response.text()) .then((responseText) => { callback(JSON.parse(responseText)); }).done(); } //get请求 static get(url, callback) { fetch(url) .then((response) => response.text()) .then((responseText) => { callback(JSON.parse(responseText)); }).done(); } } module.exports = NetUtils;
最后
以上就是甜美身影最近收集整理的关于ReactNative 请求网络数据工具类。的全部内容,更多相关ReactNative内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复