关于React Native 网络请求以及变量赋值的问题

来源:互联网 发布:巨人网络官网 编辑:程序博客网 时间:2024/05/01 18:26
//初始化赋值
constructor(props){
super(props);
this.state = {
sid:"",
pla:"说点什么吧?"
};
}
//调用函数,需要绑定this,以免在函数中无法引用当前this
render() {
return (
<View>
<View style={styles.box}>
<ToolbarAndroid
style={styles.toolbar}
title="AwesomeApp"
actions={[{title: 'Settings',icon: require('./images/article.png'), show: 'always'}]}
onActionSelected={this.AjaxPost.bind(this)}
/>
</View>
<View style={styles.box}>
<TextInput style={styles.comment} placeholder={this.state.pla} keyboardType="default" multiline />
</View>
</View>
)
}
AjaxPost(){
//调用外部的this
var _this=this;
fetch('http://file.midasjr.com/play/user/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
//form格式上传参数,需要以"key=value&key=value"形式
body: 'phone=15601815671&password=123123&sid=""&rid=4&deviceid=D14448888832484539'
})
.then((response) => response.text())
.then((responseText) => {
//解析返回的json
var back=JSON.parse(responseText);
//变量重新赋值
_this.setState({sid:back.sid,pla:back.msg});
})
.catch((error) => { alert("错误"); alert(error); }) }



0 0
原创粉丝点击