422(不可处理的实体)错误

时间:2018-04-12 11:55:03

标签: reactjs http post loopbackjs

我正在尝试传递一个名为Questions的对象,该对象具有3个属性。第一个和第三个属性是string类型,第二个属性是boolean类型。例如:{"description: "What is ecommerce" , "requireMeeting": true, "expID": "1234"}

当我尝试通过前端发送帖子请求时,我收到以下错误。 (但是当我通过loopback explorer添加一个新对象时,它工作正常)

 Failed to load resource: the server responded with a status of 422 (Unprocessable Entity)

这是addQues函数,它将一个新对象发送到数据库

   addQues(newQues) {
    console.log(newQues);
    axios.request({
      method: 'Post',
      url: 'http://localhost:3001/api/Questions',
      data: newQues
    }).then(response => {
    }).catch(err => console.log(err));
  }

这是onSubmit函数的代码,它传递要添加到addQues的对象

      onSubmit(e) {
    const newQues = {
      description: this.state.description,
      requireMeeting: this.state.requireMeeting,
      expID: this.state.expID
    };
    this.addQues(newQues);
    e.preventDefault();
}

这是完整的代码

import React, { Component } from 'react';
import axios from 'axios';
import '../Styles.scss';

class Questions extends Component {
  addQues(newQues) {
    console.log(newQues);
    axios.request({
      method: 'Post',
      url: 'http://localhost:3001/api/Questions',
      data: newQues
    }).then(response => {
    }).catch(err => console.log(err));
  }
  constructor() {
    super();
    // this.onChange = this.onChange.bind(this);
    this.onDescriptionChange = this.onDescriptionChange.bind(this);
    this.onExpIdChange = this.onExpIdChange.bind(this);
    this.onIsmeetingChange = this.onIsmeetingChange.bind(this);
    this.state = {
      description: '',
      requireMeeting: false,
      expID: ''
    };
  }
  onExpIdChange(e) {
    this.setState({
      expID: e.target.value
    });
  }
  onDescriptionChange(e) {
    this.setState({
      description: e.target.value
    });
  }
  onIsmeetingChange(e) {
    this.setState({
      requireMeeting: true
      // document.getElementById("checkbox").checked = true;
    });
  }
  onSubmit(e) {
    const newQues = {
      description: this.state.description,
      requireMeeting: this.state.requireMeeting,
      expID: this.state.expID
    };
    this.addQues(newQues);
    e.preventDefault();
  }
  render() {
    return (
      <div>
        <br/>
        <h1> DO NOT HESISTATE TO ASK OUR EXPERTS </h1>
        <form onSubmit={this.onSubmit.bind(this)}>
          <div className="input-field">
            <label htmlFor="description"> Description </label>
            <textarea rows="10" cols="100" name="description" onChange={this.onDescriptionChange} />
          </div>
          <div className="input-field">
            <input type="text" name="expID" onChange={this.onExpIdChange} />
            <label htmlFor="name"> expID </label>
          </div>
          <div className="checkbox">
            <label>
              <input type="checkbox" name="requireMeeting"
                onClick={this.onIsmeetingChange} />Meeting
            </label>
          </div>
          <input type ="submit" value="ASK" className="btn" />
        </form>
      </div>
    );
  }
}
export default Questions;

1 个答案:

答案 0 :(得分:-2)

由于http不支持布尔值,因此最好避免在POST / PUT体中使用布尔值。相反,您可以将0和1发送为false,true并相应地处理。发送0和1是一种更兼容的格式,用于通过HTTP传输布尔值。

相关问题