Ven*_*nky 5 fetch react-native react-native-android react-native-ios
我是本机反应的新手,我正在尝试向我的 API 发送一些数据,基本上是一个 POST 请求,添加了一个按钮并尝试使用它来调用获取请求,但我不确定我做错了什么这个 ??它在日志中显示“请将方法附加到此组件”,如果我在这方面做错了什么,请告诉我?
这是我的按钮
<Button
style={{height: 60,width:150}}
onClick={ () => this.submitNewCustomer()}
title="SUBMIT"
backgroundColor='#C0C0C0'
color="black"
/>
Run Code Online (Sandbox Code Playgroud)
这是我的方法
submitNewCustomer(){
fetch('http://endpoint.net/api/customerdetail', {
method: 'POST',
headers: new Headers({
'Content-Type': 'application/json', // <-- Specifying the Content-Type
}),
body: JSON.stringify({
CustomerId: '1ef87a90-a941-4ebb-b101-66f74ac07778',
CustomerName: this.state.customername,
UserId:'user2',
VehicleCompanyName:this.state.vehiclecompanyname,
VehicleModelType:this.state.vehiclemodeltype,
VehicleNumber:this.state.vehiclenumber,
CustomerImage:'',
Location:'',
CustomerImageType:'png'
}), // <-- Post parameters
})
.then((response) => response.text())
.then((responseText) => {
alert(responseText);
})
.catch((error) => {
console.error(error);
});
}
Run Code Online (Sandbox Code Playgroud)
任何输入都会非常有帮助
小智 5
你需要使用onPress而不是`onClick,所以你的按钮应该是
<Button
style={{height: 60,width:150}}
onPress={ () => this.submitNewCustomer()}
title="SUBMIT"
backgroundColor='#C0C0C0'
color="black"
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4333 次 |
| 最近记录: |