0

I'm trying to do a post of multiple points of data from a form input.

However, the form data doesn't reach json output payload (I checked the network output). It seems to never get triggered.

If there's a better way to re-write this- I am open to it

I've put it all into Codepen- http://codepen.io/yarnball/pen/LRVgpo?editors=1011

The data needs to be posted in this exact way:

{
        "title": "SAMPLE",
        "tag": [
            {
                "name": "Movie",
                "taglevel": 1,
            }
        ],
        "info": []
    }

Post method

    var Postapi = React.createClass({
componentWillMount () {
var form = document.querySelector('form')
return fetch('http://localhost:8000/api/Data/', {
 method: 'POST',
 body: JSON.stringify({
     title: this.state.itemtitle,
     tag:[
       {name:this.state.tagtitle,
       taglevel:this.state.taglevel}
      ],
     info:[]
  })
})

},

Sample return

<form onSubmit={this.handleSubmit}>
 ...
  <input
    placeholder="Item Title"
    type="text"
    itemtitle={this.state.itemtitle}
    onChange={this.handleChange}
  />

Initial state & submit

  getInitialState: function() {
  return {
    itemtitle: [],
    tagtitle: [],
    taglevel: [],
    tagoptions: Exampledata
  };
  },

  handleChange: function(event) {
    this.setState({itemtitle: event.target.itemtitle});
    this.setState({tagtitle: event.target.tagtitle});
    this.setState({tagname: event.target.tagname});
  },
  handleSubmit: function(e) {
    e.preventDefault();
    var itemtitle = this.state.itemtitle
    var tagtitle = this.state.tagtitle
    var taglevel = this.state.taglevel
    this.setState({itemtitle: '', text: ''});
  },
5
  • This is missing an end bracket JSON.stringify({ title: this.state.itemtitle, tag:[{ name:this.state.tagtitle, taglevel:this.state.taglevel, }], info:[], <<<< here Commented Sep 9, 2016 at 11:15
  • Nope that didn't help. The code tells me I need a bracket to end it with (which gives an error too). Any other advice? Commented Sep 9, 2016 at 14:25
  • }), is what you need Commented Sep 9, 2016 at 14:36
  • This has not helped. I feel like my data is not being passed from my form to my json post data either. Please see the updated codepen.io/yarnball/pen/LRVgpo Commented Sep 9, 2016 at 15:04
  • It was still a huge problem regardless Commented Sep 9, 2016 at 15:05

1 Answer 1

1

you must call your fetch function in handlesubmit function...according to this link , componentWillMount : is executed before rendering, on both server and client side. so your form is empty. you need read more about react life cycle.

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.