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

//初始化赋值
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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值