React Native - Fetch POST request is sending as GET request

后端 未结 11 1098
感情败类
感情败类 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条回答
  • 2020-12-16 15:24

    Check your OPTIONS response. It probably has a 302 redirect or similar.

    In our case, Django didn't like it if the URL didn't end in a /

    0 讨论(0)
  • 2020-12-16 15:28

    I had the same kind of issue. You have to assign the object, not sure why.

    let options = {};

    options.body = formdata;

    options.header = header;

    options.method = 'post';

    0 讨论(0)
  • 2020-12-16 15:35

    I had this issue when the POST request was to an HTTPS (rather than HTTP) server. For some reason, it would somewhere along the way be converted into a GET request.

    It turns out what I was doing incorrectly was sending the request to http://myserver.com:80 rather than to https://myserver.com:443. Once I switched it over to the proper prefix and ports, the request would properly send as a POST.

    0 讨论(0)
  • 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);
          }) 
    };
    
    0 讨论(0)
  • 2020-12-16 15:38

    This worked for me:

    let data = {
      method: 'POST',
      credentials: 'same-origin',
      mode: 'same-origin',
      body: JSON.stringify({
        appoid: appo_id
      }),
      headers: {
        'Accept':       'application/json',
        'Content-Type': 'application/json',
        'X-CSRFToken':  cookie.load('csrftoken')
      }
    }
    return fetch('/appointments/get_appos', data)
            .then(response => response.json())  // promise
            .then(json => dispatch(receiveAppos(json)))
    } 
    
    0 讨论(0)
  • 2020-12-16 15:40

    I had the same issue. In my case I had an extra / at the end of my route. I removed it and problem solved.

    http://google.com/someData/ to http://google.com/someData

    0 讨论(0)
提交回复
热议问题