反应-将正确的数据发送到服务器并获取错误

时间:2019-04-10 21:40:11

标签: javascript react-native

我正在尝试在react-native中创建一个登录名。为此,我将用户信息发送到Web服务。如果信息正确,则服务器将返回json

问题是我将正确的用户信息发送到服务器,并且收到一条错误消息,好像我输入了错误的信息一样

但是当我使用Postman进行相同信息的测试时,我没有收到任何错误消息

构造函数

constructor() {
        super()
        this.state = {
            showPass: true,
            press: false,
            username: "",
            password: ""
        }
    }

获取数据的功能

checkLogin = () => {
        const { username, password } = this.state;
        var url = 'https://xxxxxxx/xxxx/xxxx/xxxx/auth/login'

        fetch(url, {
            method: 'POST',
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                username, password
            })
        })
        .then((response) => response.json())
        .then((responseJson) => {
            if (!responseJson.errors) {
                this.props.navigation.navigate('foto');
                console.log(username + " 1");
                console.log(password + " 1");
            } else {
                Alert.alert('Error', responseJson.message, [{
                    text: 'Fechar'
                }]);
                console.log(username + " 2");
                console.log(password + " 2");

            }
        })
        .catch((error) => {
            console.error(error);
        })



输入

<TextInput onChangeText={text => this.setState({ username: text })} />
<TextInput onChangeText={text => this.setState({ password: text })} />

<TouchableOpacity style={Style.btnLogin} onPress={() => this.checkLogin()} >
        <Text style={Style.textoLogin}>Entrar</Text>
</TouchableOpacity>

我从Postman获得的JSON与来自输入的相同信息

{
    "errors": false,
    "type": "-",
    "line": "-",
    "message": "Login successfully!",
    "user": {
        "user_id": 2,
        "name": "xxxxxxx",
        "username": "xxxxxxxx",
        "email": "xxxxx@xxxxx.pt"
    },
    "_token": "xxxxxxxxxxx"
}

1 个答案:

答案 0 :(得分:0)

所以我解决了我的问题,我不得不将body更改为FormData()

代码如下:


checkLogin = () => {

        var url = 'https://xxxxx/xxxx/xxx/xxx/auth/login';
        const { username, password } = this.state;
        /*const username1 = this.state.username;
        const password1 = this.state.password;
        var data = {
            "username" : this.state.username,
            "password" : this.state.password 
        }*/
        var formData=new FormData();
        formData.append('username', username);
        formData.append('password',password);


        fetch(url, {
            method: 'POST',
           // body: JSON.stringify(data)
           body: formData
        })
        .then((response) => response.json())
        .then((responseJson) => {
            if (!responseJson.errors) {
                this.storeData(JSON.stringify(responseJson.user), responseJson._token)
                this.props.navigation.navigate('foto');

            } else {
                Alert.alert('Error', responseJson.message, [{
                    text: 'Fechar'
                }]);


            }


        })
        .catch((error) => {
            console.error(error);
        })