我正在开发一个react native android应用程序,需要从服务器获取数据,因此我使用了fetch
api。我的代码:
cashOnDelivery(){
var result = this.props.navigation.getParam('result')
result["qty"] = this.state.quantity
result["booking_date"] = this.state.date_obj
result["booking_time"] = this.state.time
result["booking_amt"] = this.state.total
var uid = result["user_id"]
var serviceid = result["service_id"]
var loc_name = result["location_name"]
var loc_lat = result["location_lat"]
var loc_long = result["location_long"]
var vehicleid = result["vehicle_id"]
var makeid = result["make_id"]
var modelid = result["model_id"]
var colorid = result["color_id"]
var vehiclereg = result["vehicle_reg_num"]
var fuelid = result["fuel_id"]
var fuelsubid = result["fuel_subtype_id"]
var qty = result["qty"]
var bookingdate = result["booking_date"]
var bookingtime = result["booking_time"]
var bookingamt = result["booking_amt"]
var formData = new FormData();
formData.append('user_id', uid);
formData.append('service_id',serviceid );
formData.append('location_name',loc_name );
formData.append('location_lat',loc_lat );
formData.append('location_long',loc_long );
formData.append('vehicle_id',vehicleid );
formData.append('make_id', makeid);
formData.append('model_id', modelid);
formData.append('color_id', colorid);
formData.append('vehicle_reg_num',vehiclereg );
formData.append('fuel_id',fuelid );
formData.append('fuel_subtype_id',fuelsubid );
formData.append('qty',qty );
formData.append('booking_date', bookingdate);
formData.append('booking_time',bookingtime );
formData.append('booking_amt', bookingamt);
formData.append('payment_method',"cash")
fetch('http://link.com', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
},
body: formData
}).then((response) => response.json())
.then((responseJson) => {
ToastAndroid.show(JSON.stringify(responseJson),ToastAndroid.LONG)
if(!responseJson.error){
// ToastAndroid.show(responseJson.message,ToastAndroid.LONG)
this.props.navigation.navigate("HomeScreen")
}else{
}
}).catch((error) => {
ToastAndroid.show(error.toString(),ToastAndroid.LONG)
});
}
但是当调用cashOnDelivery()
时,它给了我错误。网络请求失败。我的互联网连接良好,我正在使用Live服务器。为什么我要面对这个问题?