React-Native 获取node.js提供的接口
一个简单的React-Native 获取node.js提供的接口的实现
一、node.js
var http = require("http");
var url = require("url");
var querystring = require("querystring");
var json1 = '{"state":"1"}';
var json2 = '{"state":"0"}';
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/json"});
var pathname = url.parse(request.url).pathname;
var query = url.parse(request.url).query;
var userName = querystring.parse(query)["userName"];
var passWord = querystring.parse(query)["passWord"];
if(userName==='weifengzz' && passWord==='123'){
response.write(json1);
}else{
response.write(json2);
}
response.end();
}).listen(1314);
二、react-native
'use strict';
var React = require('react-native');
var FileUpload = require('NativeModules').FileUpload;
var {
StyleSheet,
Text,
View,
TouchableOpacity,
Animated,
Image,
ToastAndroid,
} = React;
var t = require('tcomb-form-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
TouchableHighlight
} = React;
var REQUEST_URL = 'http://192.168.6.5:1314/GetJson';
var Form = t.form.Form;
var Person = t.struct({
userName: t.String,
password: t.String,
});
var options = {
fields: {
password: {
placeholder: '密码',
label: '密码',
password: true,
},
userName: {
placeholder: '用户名',
label: '用户名',
}
}
};
var verification = React.createClass({
getInitialState: function() {
return {
value: {
userName: null,
password: null,
result: null,
}
};
},
onPress: function () {
var value = this.refs.form.getValue();
if (value) {
}
},
render: function() {
return (
<View style={styles.container}>
<Form
ref="form"
type={Person}
value={this.state.value}
options={options} />
<TouchableHighlight style={styles.button} onPress={this.onPress} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>登录</Text>
</TouchableHighlight>
<TouchableHighlight style={styles.button} onPress={this.onPressToUp} underlayColor='#99d9f4'>
<Text style={styles.buttonText}>上传文件</Text>
</TouchableHighlight>
<TouchableHighlight onPress ={ () => ToastAndroid.show('请选择图片', ToastAndroid.SHORT) }
style={styles.button} underlayColor='#99d9f4' >
<Text style={styles.buttonText}>提醒</Text>
</TouchableHighlight>
</View>
);
},
componentDidMount: function() {
this.fetchData('weifengzz','123');
},
fetchData: function(un,pw) {
fetch(REQUEST_URL, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
userName: un,
password: pw,
})
})
.then((response) => response.json())
.then((responseData) => {
this.setState({
result: responseData,
});
})
.done();
},
responseData: function(response){
return response.result.data;
}
});
var styles = StyleSheet.create({
container: {
justifyContent: 'center',
marginTop: 50,
padding: 20,
backgroundColor: '#ffffff',
},
title: {
fontSize: 30,
alignSelf: 'center',
marginBottom: 30
},
buttonText: {
fontSize: 18,
color: 'white',
alignSelf: 'center'
},
button: {
height: 36,
backgroundColor: '#48BBEC',
borderColor: '#48BBEC',
borderWidth: 1,
borderRadius: 8,
marginBottom: 10,
alignSelf: 'stretch',
justifyContent: 'center'
}
});
AppRegistry.registerComponent('verification', () => verification);
React-Native 获取node.js提供的接口的更多相关文章
- [RN] React Native 获取地理位置
React Native 获取地理位置 实现原理: 1.用 navigator.geolocation.getCurrentPosition 获取到坐标信息 2.调用 高德地图 接口,解析位置数据 ...
- [RN] React Native 获取验证码 按钮
React Native 获取验证码 按钮 效果如图: 实现方法: 一.获取验证码 按钮组件 封装 CountDownButton.js "use strict"; import ...
- Node.js提供了哪些内容(API)
Nodejs运行时,JavaScript代码运行时的环境. 提供了一些核心模块,应用程序编程接口(Application Program Interface,API) API 的意思就是.一个已经解 ...
- React Native 系列(一) -- JS入门知识
前言 本系列是基于React Native版本号0.44.3写的,最初学习React Native的时候,完全没有接触过React和JS,本文的目的是为了给那些JS和React小白提供一个快速入门,让 ...
- [React Native]获取网络状态
使用React Native,可以使用NetInfo API获取手机当前的各个网络状态. componentWillMount() { NetInfo.fetch().done((status)=&g ...
- node.js:《接口实现文件的上传和下载》
使用node.js写上传文件和下载文件的接口 上传接口: 开始写接口前,我们先安装一个上传文件的插件:npm install multer 安装成功在package.json或package-lock ...
- React Native 获取网络数据
getMoviesFromApiAsync() { return fetch('http://facebook.github.io/react-native/movies.json') .then(( ...
- React Native 获取组件(Component)在屏幕上的位置
年后主客户端的需求以及老的业务迁移RN,现在疯狂的在学RN.在迁移需求的时候遇到需要获取组件在屏幕上的绝对位置.页面如下: 就需要展开的时候获取sectionHeader(默认排序)在屏幕上的具体位置 ...
- react native 获取 软键盘高度 和 新增软键盘的组件
import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Text, View, Keyboard, Te ...
随机推荐
- 改进的简单Tooltips显示
使用js简单改进了Tooltips的显示效果,可进一步使用CSS对改进的Tooltips进行美化. 前台布局代码: <asp:Panel ID="Panel1" runat= ...
- scrollview不能滚动
1. 图片视图上不能直接滚动,需要设置交互属性为YES _contentView = [[UIImageView alloc]initWithFrame:CGRectMake(0, _headerVi ...
- Web 1三级联动 下拉框 2添加修改删除 弹框
Web 三级联动 下拉框 using System; using System.Collections.Generic; using System.Linq; using System.Web; u ...
- (转)ultraedit for linux 安装与注册破解
http://hi.baidu.com/simonwoos_zone/item/93f88b41dbad48e41e19bcc8
- 【转】nginx之主配置文件:root和alias
nginx指定文件路径有两种方式root和alias,这两者的用法区别,使用方法总结了下,方便大家在应用过程中,快速响应.root与alias主要区别在于nginx如何解释location后面的uri ...
- php 去掉 头尾 空格 2种方法
看似很简单的问题,其实还是有点坑的,首先这里 空格转义,不是字符串,直接用trim()是去不掉. 1,用preg_replace替换 $test = " dfadad 论责民与三英的关系77 ...
- HTML基本标签大全
HTML标签 <h#></h#>标题标签<hr/>水平线,单标记<p></p>段落标签  空格<i></i ...
- 在Docker上部署使用Azure CLI镜像
Docker是非常流行的容器技术,在Docker中安装部署多种工具非常快速和方便:而Azure CLI是微软提供的可以在Linux/Mac上运行的跨平台命令行管理工具,本文介绍如何在Azure上安装部 ...
- linux中如何使用微软鼠标的第4、5键
虽说使用linux的 人大都对微软没什么好感,但不能否认微软确实也出了不少好东西呀,比如微软鼠标(IE系列) icon_smile.gif IE 2.0和以上版本都有5个按钮,除了正常的左中右外,两侧 ...
- Delphi泛型评测(30篇)
http://www.cnblogs.com/jxgxy/category/216671.html