nguyli03
nguyli03

Reputation: 51

Axios post does not send data

I am using an axios post to send my information to the backend using Python flask. I use ReactJS as my front end. Here's my call:

  sendInfo(){
    var config = { headers: {
                  'Content-Type': 'application/json',
                  'Access-Control-Allow-Origin': '*'}
              }

    axios.post('/myprofileC',{
      department:this.state.department;
      student: this.state.student
    },config)
    .then(function (response) {
         if (response.data == '/myprofile'){
             console.log(this.state);
             alert('Your changes have been saved');
      }
          window.location = '/myprofile';
      })
     .catch(function (error) {
         console.log(error);
     });
    }
  }

I have used the exact same code (with different data) for 2 other pages and it works just fine. However, for this one page, the data did not get sent at all. when I try printing thing out from Python end, I get an empty dictionary.

Does anyone have any idea on what is happening?

Upvotes: 1

Views: 5161

Answers (1)

chipit24
chipit24

Reputation: 6987

You should not set the Access-Control-Allow-Origin header, this is something that is sent in the server response. You also have a syntax error in your code. The following is not a valid JavaScript object:

{
  department: this.state.department;
  student: this.state.student
}

Replace the semicolon (;) with a comma (,). Everything else looks ok to me.

Familiarize yourself with the network tab in Chrome dev tools: https://developers.google.com/web/tools/chrome-devtools/network-performance/reference. If the above request was indeed executed, this will give you exact details of the request and response.

Upvotes: 5

Related Questions