请求失败TypeError:网络请求失败

时间:2020-06-25 11:28:19

标签: rest react-native android-studio asp.net-web-api android-emulator

我收到此请求失败TypeError: Network request failed error请求失败TypeError:网络请求失败 在EventTarget.xhr.onerror(C:\ reactNative \ scanandgo \ node_modules \ whatwg-fetch \ dist \ fetch.umd.js:473) 在EventTarget.dispatchEvent(C:\ reactNative \ scanandgo \ node_modules \ event-target-shim \ dist \ event-target-shim.js:818) 在EventTarget.setReadyState(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:574) 在EventTarget .__ didCompleteResponse(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:388)中 在C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ Network \ XMLHttpRequest.js:501 在RCTDeviceEventEmitter.emit(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ vendor \ emitter \ EventEmitter.js:189) 在MessageQueue .__ callFunction(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:436) 在C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:111 在MessageQueue .__ guard(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:384) 在MessageQueue.callFunctionReturnFlushedQueue(C:\ reactNative \ scanandgo \ node_modules \ react-native \ Libraries \ BatchedBridge \ MessageQueue.js:110)

import React, { Component } from 'react';
import {
    StyleSheet, Text, View, Image, TouchableWithoutFeedback, StatusBar,
    TextInput, SafeAreaView, Keyboard, TouchableOpacity, KeyboardAvoidingView, ScrollView, Button, AsyncStorage
} from 'react-native';

export default class SignUp extends Component {
    static navigationOptions = ({ navigation }) => ({
        title: 'SignUp',
    })
    constructor() {
        super();

        this.state = {
            FirstName: '',
            LastName: '',
            Email: '',
            Password: '',
        }

        this.FirstName = this.FirstName.bind(this);
        this.LastName = this.LastName.bind(this);
        this.Email = this.Email.bind(this);
        this.Password = this.Password.bind(this);
        this.register = this.register.bind(this);

    }
    FirstName(event) {
        this.setState({ FirstName: event.target.value })
    }

    LastName(event) {
        this.setState({ LastName: event.target.value })
    }

    Email(event) {
        this.setState({ Email: event.target.value })
    }
    Password(event) {
        this.setState({ Password: event.target.value })
    }
    register(event) {
        return fetch('https://192.168.56.1:44304/Api/login/InsertUser', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            },
            body: JSON.stringify({
                FirstName: this.state.FirstName,
                LastName: this.state.LastName,
                Email: this.state.Email,
                Password: this.state.Password
            })
        }).then((response) => response.json())
        .then(json => {
            alert('Request succeeded with JSON response', data);
          }).catch(function (error) {
            console.log('Request failed', error);
          });
    }

    render() {
        return (
            //<SafeAreaView style={styles.container}>
            <View style={styles.container}>
                <StatusBar barStyle="light-content" />

                <TouchableWithoutFeedback style={styles.container} onPress={Keyboard.dismiss}>
                    <View style={styles.logoContainer}>
                        <View style={styles.logoContainer}>
                            <Image style={styles.logo}
                                source={require('../images/LOGO.png')}
                            >
                            </Image>
                            <Text style={styles.title}>Welcome to ScanAndGo</Text>
                        </View>
                        <View style={styles.infoContainer}>
                            <TextInput style={styles.input}
                                name="name"
                                placeholder="Enter First Name"
                                onChange={this.FirstName}
                                placeholderTextColor='rgba(255,255,255,0.8)'
                                keyboardType='name-phone-pad'
                                returnKeyType='next'
                                autoCorrect={false}
                                onSubmitEditing={() => this.refs.txtEmail.focus()}
                            //onChangeText= {first_name => setState({first_name})}

                            />
                            <TextInput style={styles.input}
                                placeholder="Enter Lastname"
                                placeholderTextColor='rgba(255,255,255,0.8)'
                                onChange={this.LastName}
                                keyboardType='name-phone-pad'
                                returnKeyType='next'
                                autoCorrect={false}
                                onSubmitEditing={() => this.refs.txtEmail.focus()}
                            //onChangeText= {last_name => setState({last_name})}
                            />
                            <TextInput style={styles.input}
                                placeholder="Enter email address"
                                placeholderTextColor='rgba(255,255,255,0.8)'
                                keyboardType='email-address'
                                returnKeyType='next'
                                autoCorrect={false}
                                onChange={this.Email}
                                ref={'txtEmail'}
                                onSubmitEditing={() => this.refs.txtPassword.focus()}
                            //onChangeText= {email => setState({email})}
                            />
                            <TextInput style={styles.input}
                                placeholder="Enter password"
                                placeholderTextColor='rgba(255,255,255,0.8)'
                                returnKeyType='next'
                                onChange={this.Password}
                                secureTextEntry
                                autoCorrect={false}
                                ref={'txtPassword'}
                                onSubmitEditing={() => this.refs.txtPasswordRepeat.focus()}
                            //onChangeText= {password => setState({password})}
                            />
                            {/* <TextInput style={styles.input}
                                placeholder="Reenter password"
                                placeholderTextColor='rgba(255,255,255,0.8)'
                                returnKeyType='go'
                                secureTextEntry
                                autoCorrect={false}
                                ref={'txtPasswordRepeat'}
                            /> */}
                            <Button
                                title="REGISTER"
                                onPress={this.register}
                            //onPress={() => this.props.navigation.navigate('Login')}
                            />
                            <View />
                            <Button
                                title="Already registered? Sign In"
                                color='rgb(32, 53, 70)'
                                onPress={() => this.props.navigation.navigate('Login')}
                            />

                        </View>
                    </View>
                </TouchableWithoutFeedback>

            </View>
            //</SafeAreaView>
        )
    }
}

这是我的JS代码和在ASP.NET上运行的REST API。我可以在模拟器浏览器中访问我的API,所以看起来不是API问题,而是我的获取方法有问题。

谢谢

0 个答案:

没有答案