簡體   English   中英

React,JS語法錯誤400請求錯誤

[英]React,js bad syntax 400 request bug

我正在嘗試以響應方式向我的API發送一個Post請求,但是我收到了錯誤的語法錯誤。 我在StackOverflow上提到了這篇文章,但是我看不出我的代碼有什么區別。

React.js,如何將多部分/表單數據發送到服務器

export default class CapsuleForm extends React.Component {
  constructor (props) {
    super(props);

    this.state = {
      userUrl: this.props.auth.domain + "/users/" + this.props.userID,
      dateToPost: moment(),
      image: "",
      caption: "",
      redirect: false
    };

    this.handleChange = this.handleChange.bind(this);
    this.handleFormSubmit = this.handleFormSubmit.bind(this);
    this.handleDateChange = this.handleDateChange.bind(this);
  }

  handleFormSubmit(e) {
    e.preventDefault();
    var form = new FormData()
    form.append("user", this.state.userUrl);
    form.append("dateToPost", this.state.dateToPost.format());
    form.append("image", this.state.image.files[0]);
    form.append("caption", this.state.caption);

    for (var pair of form.entries()) {
       console.log(pair[0]+ ', ' + pair[1]);
    }

    this.props.auth.postCapsule(
      form
    ).then(() => {
       this.setState({redirect: true})
    })
    .catch(err =>{
        alert(err);
    })
  }

  handleDateChange(date) {
    this.setState({
      dateToPost: date
    });
  }

  handleChange(e) {
    this.setState(
         {
             caption: e.target.value
         }
     )
  }

  render() {
    function FieldGroup({ id, label, help, ...props }) {
      return (
        <FormGroup controlId={id}>
          <ControlLabel>{label}</ControlLabel>
          <FormControl {...props} />
        </FormGroup>
      );
    }

    return (
      <Form onSubmit={this.handleFormSubmit.bind(this)}>
        <h1> Create a SnapCapusle </h1>
        {/* TODO Find out why FieldGroup component does not pass onChange function
         correctly */}
        <ControlLabel> Date </ControlLabel>
        <DatePicker
          selected={this.state.dateToPost}
          onChange={this.handleDateChange}
          style={ css }
          showTimeSelect
          dateFormat="LLL"
        />
        <FormGroup id="1">
          <ControlLabel> File </ControlLabel>
          <FormControl
            type="file"
            label="Image"
            name="image"
            inputRef={ref => {
              this.state.image = ref;
            }}
          />
        </FormGroup>
        <FormGroup id="2">
          <ControlLabel> Caption </ControlLabel>
          <FormControl
            type="text"
            placeholder="Enter text"
            name="caption"
            onChange={this.handleChange}
          />
        </FormGroup>
        <FormGroup>
            <Button type="submit"> Submit </Button>
        </FormGroup>
      </Form>
    );
  }
}

這是PostCapsule函數。

postCapsule(formData) {
      console.log("PostCapsule called to " + `${this.domain}/snapcapsule/`)
      return fetch(`${this.domain}/snapcapsule/`, {
          method: 'POST',
          credentials: 'same-origin',
          body: formData
      })
    }

這是服務器上成功的Post Request的樣子 成功發布請求

這是打印到控制台的javascript代碼。 服務器發送數據的瀏覽器日志

我通過在控制台中查看http錯誤解決了這個問題。 如果轉到“網絡”選項卡,則可以單擊該請求,這將給您一條錯誤消息。 錯誤消息告訴我,沒有與我提供的網址匹配的網址。 我意識到我忘記了用“ /”結尾的userURL。

在此處輸入圖片說明

this.state = {
    userUrl: this.props.auth.domain + "/users/" + this.props.userID + "/",
    dateToPost: moment(),
    image: "",
    caption: "",
    redirect: false
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM