如何将我的React表单数据解析为一个对象并通过AJAX调用发送?

时间:2019-03-27 09:42:25

标签: javascript ajax reactjs styled-components

我正在开发具有7个输入的彩票应用程序:6个用户号码和开奖次数。

我正在尝试将所有输入字段中的数据解析为“结果”对象,并通过AJAX调用将该数据发送到将处理该数据的服务器。我还使用React样式的组件。

主要问题是从输入字段中获取所有数据并将其推送到“结果”对象。

我知道我应该将点击处理程序添加到我的按钮中,但是我不知道应该在此方法中添加什么内容。

这是我的代码:

import React from 'react';
import styled from 'styled-components';

const InputField = styled.form`
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    margin: 100px, 0px;
    padding: 100px, 0px;
    align-items: center;
`
const InputWrapper = styled.div`
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
`
const Label = styled.label`
    display: block;
    text-align: center;
    width: 100px;
    height: 50px;
`
const Input = styled.input`
    display: block;
    text-align: center;
    width: 100px;
    height: 50px;
`
const Button = styled.button`
    border: 1px solid #23723c;
    color: #fdfdfd !important;
    letter-spacing: 3px;
    background: rgb(11, 56, 33);
    font-weight: 700;
    margin-top: 30px;
    padding: 20px;
    text-transform: uppercase;
    border-radius: 6px;
    display: inline-block;
    width: 200px;
`


export default class InputArea extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      value: '',
      results : {
        numbers: [],
        draws: ''
      }
    }
  }

  /* handledrawsChange = event => {
    this.setState({
      event
    })
  } */


  drawsHandler = event => {
    this.setState({...this.state.results, draws: parseInt(event.target.value)});
    //let results = {...this.state.results}
    //results.draws = parseInt(event.target.value);
    //this.setState(results);
    console.log(this.state);
  }

  numbersHandler = event => { 
    this.setState({number: this.state.results.numbers.push(parseInt(event.target.value))});
    console.log(this.state);
  }

  componentDidMount() {
    fetch("http://localhost:8080/simulate", {
      method: "POST",
      body: this.state.results
    });
  }


  render() {
      return (
        <InputField>
          <InputWrapper>
            <Label>1st number</Label>
            <Input name="user_number_1" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>2nd number</Label>
            <Input name="user_number_2" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>3rd number</Label>
            <Input name="user_number_3" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>4th number</Label>
            <Input name="user_number_4" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>5th number</Label>
            <Input name="user_number_5" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>6th number</Label>
            <Input name="user_number_6" type="number" maxlength="2" value={this.state.value} onChange={this.numbersHandler} required></Input>
            <Label>Number of draws:</Label>
            <Input name="draw_number" type="number" value={this.state.value} onChange={this.drawsHandler} required></Input>
          </InputWrapper>
          <Button type="submit" onClick={this.drawsHandler}>Let's win!</Button>
        </InputField>
      )
  }
}

0 个答案:

没有答案