开机自检错误 400 "beer name must be defined"



我有一个事件处理程序,用于将条目提交到 API 中。API 是一个啤酒列表,我正在尝试通过 ON ADD 方法将我的条目提交回 API。当我提交我的条目时,我收到一个 400 错误,但没有一个错误的请求。错误预览显示错误:"必须定义啤酒名称。

我相信我的问题在于我的 JSON 字符串参数,但我不确定在该字段中放置什么。一些建议会很棒。谢谢。

 onSubmit(event) {
    event.preventDefault();
    fetch("https://beer.fluentcloud.com/v1/beer", {
      body: JSON.stringify(),
      headers: {
        "Accept" : "application/json",
        "Content-Type": "application/json"
      },
      method: "POST"
    });
    this.props.onAdd(this.nameInput.value, this.likesInput.value);
    this.nameInput.value = '';
    this.likesInput.value = '';
  }

您发布一个空的正文,请将带有所需字段的对象填充在那里,例如

JSON.stringify({
 name: this.nameInput.value,
 likes: this.likesInput.value
});

另请注意,fetch API 返回 Promise ,所以我会修改您的代码以等待它解决:

event.preventDefault();
fetch("https://beer.fluentcloud.com/v1/beer", {
  body: JSON.stringify({
    name: this.nameInput.value,
    likes: this.likesInput.value
  }),
  headers: {
    "Accept" : "application/json",
    "Content-Type": "application/json"
  },
  method: "POST"
}).then(() => {
  console.log('Submitted!');
  this.props.onAdd(this.nameInput.value, this.likesInput.value);
  this.nameInput.value = '';
  this.likesInput.value = '';
}).catch(e => console.error('Failed', e));

相关内容

最新更新