Reactjs 400在react native中使用Axios从API提取时出错

Reactjs 400在react native中使用Axios从API提取时出错,reactjs,react-native,axios,Reactjs,React Native,Axios,我在使用Axios获取react native中的API时遇到问题。web API对于grant_类型来说有点复杂,这是一个巨大的问题。此外,是否需要在xcode上进行任何配置以允许HTTP资源 import React, { Component } from 'react'; import { Text, TouchableOpacity, View, TextInput, StyleSheet, } from 'react-native';

我在使用Axios获取react native中的API时遇到问题。web API对于grant_类型来说有点复杂,这是一个巨大的问题。此外,是否需要在xcode上进行任何配置以允许HTTP资源

import React, { Component } from 'react';
import { 
    Text, 
    TouchableOpacity,
    View,
    TextInput,
    StyleSheet,
 } 
 from 'react-native';
 import axios from 'axios';

 export default class Login extends Component {
     constructor(props){
         super(props)
       this.state =  {
                username:'',
                password:''
         };
     }
     componentDidMount(){
         const data={
             grant_type: 'password',
             client_id:'RxPadApp',
             username: this.state.username,
             password: this.state.password,

         }
         axios.post('http://192.168.210.159:3000/api/2019/token',
             data
         )
         .then((response)=>{
             console.log(response);
         }).catch((error)=>{
             console.log(error);
         });
     }
     render(){
         return(
            <View style ={styles.container}>
                <View style={styles.logCon}>
                    <Text style={styles.loginText}>Login</Text>
                </View>
                <Text style= {styles.label}>Username</Text>
                <TextInput 
                  style={styles.textbox}  
                  autoCapitalize="none"
                  placeholder="Username"
                  onChangeText= {(username)=>{
                       this.setState({username});
                  }
                  }
                  value={this.state.username}/>

                <Text style= {styles.label}>Password</Text> 
                <TextInput 
                  style={styles.textbox}  
                  placeholder="Password"
                  secureTextEntry={true}
                  onChangeText= {(password)=>{
                       this.setState({password});

                  }

                  }
                  value={this.state.password}
                />

                <View>
                    <Text>Forgot password</Text>
                </View>
                <TouchableOpacity style={styles.signin}

                >
                    <Text style={styles.signinText}>Sign In</Text>
                </TouchableOpacity>
                <View style={styles.signupTextCont}>
                    <Text style={styles.signupText}>Not an APG Member? </Text>
                    <Text 
                    style={{fontWeight:'bold', color: '#1A78B9'}}
                    onPress={() => this.props.navigation.navigate('SignupScreen')}
                    >
                    Sign Up
                    </Text>
                </View> 
            </View>
         );
     }
 }
这是url:这是授权类型:授权类型=密码和用户名=mary2@ApgDemo.ca&password=demodemo&client_id=RxPadApp,在邮递员的身体里。标题包括content type=application/x-www-form-urlencoded。 我打算登录到一个主页

同时看着控制台。它显示错误代码400,这意味着代码有问题,或者由于文章正文中指定的grant_类型,URL未正确绑定。附件供您阅读


非常感谢您的帮助

根据您的邮递员示例,您发送的请求内容类型为text/plain,因此您可以在axios请求中设置相应的请求选项:

const data = 'grant_type=password&username=mary2@ApgDemo.ca&password=demodemo&client_id=RxPadApp';   
axios.post('http://192.168.210.159:3000/api/2019/token', data, { headers: { 'Content-Type': 'text/plain' }
});

那个么,什么是EXACT问题呢?它是从控制台显示错误,我不知道如何重定向到主页。虽然我还没有创建它,但您可以尝试在标题中设置内容类型。post“/myurl”,“mymessagetext”,“{headers:{'contenttype':'text/plain'};Thak@ducmai如何从表单中传递用户输入的用户名和密码?请告诉我我需要的帮助不是一个非常描述性的标题。做一个好公民,花时间写一个有意义的标题。我希望用户名和密码是用户输入的条目,因为我将创建更多的用户。我该怎么做呢?谢谢。请问我如何向它传递数据