Unable to post properly to php sever - React Native

北慕城南 提交于 2020-01-25 09:43:07

问题


I am having issues posting data to php server. Here is a snapshot of my code. Sometimes it works and post successfully and sometimes it does not.

    < TouchableOpacity
style = {{ fontSize: 18, color: 'white' }}
containerStyle = {{
    padding: 8,
        marginLeft: 70,
            marginRight: 70,
                height: 40,
                    borderRadius: 6,
                        backgroundColor: 'mediumseagreen'
}}
onPress = {() => {

    if (this.state.newTodo == "") {
        alert("Please enter a reason for appointment");
        return false
    }

    var query = {
        Reason: this.state.newTodo,
        BranchRef: this.props.branch,
        AppointmentDate: this.props.date,
        ToSeeRef: 369,
        PatientRef: 63,
        AppointmentTimeID: this.props.appointmentTime,
        AppointmentPlatform: 2,
        Completed: 0
    }
    console.log(query)

    return fetch('url', {
        method: 'POST',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(query)
    })
        // .then((response) => console.log(response))
        .then((response) => response.text())
        .then((text) => {
            if (Platform.OS === 'android') {
                text = text.replace(/\r?\n/g, '').replace(/[\u0080-\uFFFF]/g, ''); // If android , I've removed unwanted chars. 
            }
            return text
        })
}}
>
    <View style={styles.submitButton}>
        <Text style={styles.submitButtonText}>BOOK APPOINTMENT</Text>
    </View>
</TouchableOpacity >

This is the log of the response

{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "f90a0dc3-02be-45e6-a600-41a2cc53a9e8", "offset": 0, "size": 89967}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "f90a0dc3-02be-45e6-a600-41a2cc53a9e8", "offset": 0, "size": 89967}}, "headers": {"map": {"cache-control": "no-cache, private", "content-length": "89967", "content-type": "text/html; charset=UTF-8", "date": "Thu, 28 Nov 2019 16:35:23 GMT", "phpdebugbar-id": "fcebec1aec584329cabe4020a9122113", "server": "Microsoft-IIS/8.5", "x-powered-by": "ASP.NET", "x-ratelimit-limit": "60", "x-ratelimit-remaining": "59"}}, "ok": false, "status": 500, "statusText": undefined, "type": "default", "url": "https://visioncapital.officemate.ng/api/store_appointment"}

Please what could I be doing wrong?


回答1:


I was able to resolve this by changing the date format on my app.

I am using react-native-datepicker and the date format was different from what the server was expecting.

    <DatePicker
                    date={this.state.date}
                    mode="date"
                    format="YYYY-MM-DD"
                    confirmBtnText="Confirm"
                    cancelBtnText="Cancel"
                    customStyles={{
                        dateIcon: {
                            position: 'absolute',
                            left: 0,
                            top: 4,
                            marginLeft: 0
                        },
                        dateInput: {
                            marginLeft: 36,
                            borderWidth: 0,
                            right: 0,
                            color: "grey"
                        }
                    }}
                    onDateChange={(date) => { this.setState({ date: 
                    date }); }}
                />


来源:https://stackoverflow.com/questions/59093300/unable-to-post-properly-to-php-sever-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!