React Native - Fetch POST request is sending as GET request

后端 未结 11 1136
感情败类
感情败类 2020-12-16 14:52

I\'m having issues when using FETCH.

I am trying to make a POST request using FETCH in react-native.

    fetch(\"http://www.example.co.uk/login\", {
         


        
11条回答
  •  猫巷女王i
    2020-12-16 15:35

    I have made some changes and tested it, works fine for me, check below all the changes:

     constructor(props) {
        super(props);
        this.state = { isLoading: true};
        this.getRemoteData();
     }
    
    getRemoteData = () => {
    
    fetch('http://www.example.co.uk/login', {
                method: 'POST',
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    username: 'test',
                    password: 'test123',
                })
            })
    
                .then((response) => response.json())
                .then((responseData) => {
                    console.log("RESULTS HERE:", responseData)
    
                this.setState({
              isLoading: false,
              dataSource: responseJson,
            }, function(){
    
            });
          })
          .catch((error) =>{
            console.error(error);
          }) 
    };
    

提交回复
热议问题