2

When I try to post to the other server using axios it first sends OPTIONS request then POST. When I check network connection it sends object with fields I wanted just the this.state.array is empty.

It's weird beacuse when I debug it just before sending it is not empty?

var someProperty="value";

const objectToSend = {
  content: this.state.myArray,
  prop1: someProperty,
  abc:'3456'
};

console.log('user obj------->',objectToSend); //  everything is fine { content: Array(0), prop1: 'value', abc:'3456'}

  axios.post('http://server:8544/api/post', objectToSend)
          .then(function (response) {

            }) //  it sends { content: [], prop1: someProperty, abc:'3456'}

//method which updates state of array(or key value pair to be precise)

  processAnswer(q, a) {

var key = q;
var val = a;

this.state.myArray['\"'+key+'\"'] = val;

}

5
  • 1
    Nothing shown would tell us why it shouldn't be empty Commented Sep 7, 2018 at 13:26
  • Please share where you're updating your state. This would be helpful Commented Sep 7, 2018 at 13:46
  • Is the POST request going or is it stopping after OPTIONS request? Commented Sep 7, 2018 at 13:48
  • Child components receive method from main class an then they send those changes back to the method which is located in the same class where post occurs it's called processAnswer @MithunGS Post goes on and gets 200 back , other values except array are set succesfully Commented Sep 7, 2018 at 14:16
  • where are you setting values to the array? Commented Sep 10, 2018 at 7:26

1 Answer 1

2

Solved

myArray was defined as array under state myArray : []

problem was that axios didn't recognize that and although state of myArray before post was ["key":"value","key2":"val2"] after submiting it would be sent like this myArray : []

So I changed initial state to myArray : {} and now it sends key-value object {"key":"value","key2":"val2"} instead of array and everything works fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.