一、代码

 import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native'; //默认应用的容器组件
export default class App extends Component { //构造函数
constructor(props) {
super(props);
this.state = {
responseText: null
};
} //渲染
render() {
return (
<View style={styles.container}>
<Text style={styles.item} onPress={this.doXMLHttpRequest.bind(this)}>获取数据</Text>
<Text>{this.state.responseText}</Text>
</View>
);
} //使用XMLHttpRequest请求数据
doXMLHttpRequest(){
var request = new XMLHttpRequest();
request.onreadystatechange = (e) => {
if (request.readyState !== 4) {
return;
} if (request.status === 200) {
alert("请求成功!");
this.setState({responseText: request.responseText})
console.log(request.responseText); } else {
alert("请求失败!");
}
}; request.open('GET', 'https://httpbin.org/get');
request.send();
}
} //样式定义
const styles = StyleSheet.create({
container:{
flex: 1,
marginTop:25
},
item:{
margin:15,
height:30,
borderWidth:1,
padding:6,
borderColor:'#ddd',
textAlign:'center'
},
});

二、效果图

React native中使用XMLHttpRequest请求数据的更多相关文章

  1. React native 中使用Fetch请求数据

    一.代码 import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View } from ' ...

  2. React Native中的网络请求fetch和简单封装

    React Native中的网络请求fetch使用方法最为简单,但却可以实现大多数的网络请求,需要了解更多的可以访问: https://segmentfault.com/a/1190000003810 ...

  3. 在React Native中,使用fetch网络请求 实现get 和 post

    //在React Native中,使用fetch实现网络请求 /* fetch 是一个封装程度更高的网络API, 使用了Promise * Promise 是异步编程的一种解决方案 * Promise ...

  4. react native中如何往服务器上传网络图片

    let common_url = 'http://192.168.1.1:8080/'; //服务器地址 let token = ''; //用户登陆后返回的token /** * 使用fetch实现 ...

  5. 《React Native 精解与实战》书籍连载「React Native 中的生命周期」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  6. 解决React Native使用Fetch API请求网络报Network request failed

    问题来源: 1 . 在测试fetch数据请求时,Xcode9.0以上的无法请求https, 需要在Xcode中加载项目后修改Info.plist的相关配置,具体如下参考 问题及解决方法一模一样,不再重 ...

  7. React Native中的远程调试是不可靠的

    一.原因 当您发现rn app在关闭远程调试后,一些功能无法正常工作时,这很可能是由于设备上的JavaScript执行环境与远程调试器之间的细微差别造成的. 例如,日期问题,Date构造函数似乎接受C ...

  8. 在 React Native 中使用 Redux 架构

    前言 Redux 架构是 Flux 架构的一个变形,相对于 Flux,Redux 的复杂性相对较低,而且最为巧妙的是 React 应用可以看成由一个根组件连接着许多大大小小的组件的应用,Redux 也 ...

  9. react native 中的ListView

    ListView 的运用: 1.首先在react native中引入这个组件: 2.初始化的ListView 的相关属性: constructor(props) { super(props); con ...

随机推荐

  1. js的短路

    短路问题经常是发生在逻辑运算符中的逻辑与(&&).逻辑或(||) 1.逻辑或(||) 他是一真则真,当逻辑或前面的表达式结果为1或true时,逻辑或后面的表达式是不会执行的 2.逻辑与 ...

  2. react生命周期-新增与替换

    class A extends React.Component { // 用于初始化 state constructor() {} // 用于替换 `componentWillReceiveProps ...

  3. spoj1026Favorite Dice

    题意翻译 一个n面的骰子,求期望掷几次能使得每一面都被掷到. 题目描述 BuggyD loves to carry his favorite die around. Perhaps you wonde ...

  4. Assignments 1

    跑了一下之前写的UiPath Assignments脚本,没什么问题,发上来供大家参考. 先看结果: github:https://github.com/WendyZheng1022/RPA 有时间我 ...

  5. hello2 源码分析

    1.GreetingServlet.java(问候页面): /** * Copyright (c) 2014 Oracle and/or its affiliates. All rights rese ...

  6. web前端学习python之第一章_基础语法(一)

    web前端学习python之第一章_基础语法(一) 前言:最近新做了一个管理系统,前端已经基本完成, 但是后端人手不足没人给我写接口,自力更生丰衣足食, 所以决定自学python自己给自己写接口哈哈哈 ...

  7. ES6的let

    1.let声明的变量只在它所在的代码块有效. 例子: var a = [];for (var i = 0; i < 10; i++) {a[i] = function () {console.l ...

  8. Elixir东游记/上:intro/1

    1. 为啥前面还在搞haxe,现在又换到elixir了? erlang本来我就在用,用elixir不过是方便顺手给人科普而已. 2. so,接下来你打算用elixir干嘛? 很简单,写一个简单的解释器 ...

  9. Factorial(hdu 1124)

    Description The most important part of a GSM network is so called Base Transceiver Station (BTS). Th ...

  10. 中国地图插件世界地图||jQuery矢量SVG地图插件JVectorMap

    中国地图插件|jQuery矢量SVG地图插件JVectorMap https://www.html5tricks.com/jquery-china-map.html 世界地图: https://www ...