首页 > 解决方案 > 为什么在编辑项目后我的数据会附加到我的 url?

问题描述

我正在构建一个简单的食谱集合应用程序,并且遇到了一个非常奇怪的场景,当我编辑食谱项目时,新数据会附加到我的 localhost:3000/ url。例如,我原来的食谱项目数据是:

“标题:测试”,“成分:测试”,“烹饪指南:测试”

但是在我编辑数据之后,新数据也附加到了我的 localhost:3000/ url。附加到 localhost url 截图的数据。我的目标不是将此数据附加到我的 url 并留在 localhost:3000。谢谢,任何帮助将不胜感激。

这是我的放置请求代码:

updateRecipe = (e) => {
    console.log(this.props.uniqueID);
    const recipe = {
      title: this.state.title,
      ingredients: this.state.ingredients,
      summary: this.state.summary
    }

    const url = `http://localhost:5000/recipes/${this.props.uniqueID}`;
    axios.put(url, recipe)
      .then(res => this.setState({
        recipes: [...this.state.recipes, res.data],
      },
        () => console.log('recipe edited: ', res.data)
      ));
  }
<form className="form-container" onSubmit={this.updateRecipe}>
            <TextField
              style={inputStyle}
              className="form-input-title"
              type="text"
              onChange={this.onChangeHandler}
              name="title"
              id="standard-basic"
              label="Title"
              value={this.state.title}
            />
            <TextField
              style={inputStyle}
              className="form-input-ingredients"
              type="text"
              onChange={this.onChangeHandler}
              name="ingredients"
              id="standard-basic"
              label="Ingredients"
              value={this.state.ingredients}
            />
            <TextField
              id="outlined-multiline-static"
              label="Directions"
              multiline
              rows={6}
              variant="outlined"
              onChange={this.onChangeHandler}
              name="summary"
              className="form-input-directions"
              style={inputStyle}
              value={this.state.summary}
            />
            <Fab style={style} className="add-recipe-button" color="primary" aria-label="add" type="submit">
              <AddIcon />
            </Fab>
 </form>

标签: ajaxreactjsmongodb

解决方案


如果您对表单使用 onSubmit 处理程序,这是默认行为。如果这不是您要查找的内容,请不要在表单上使用 onSubmit 处理程序,而是对提交按钮使用 onClick 处理程序来调用 updateRecipe 功能。然后不修改 Url。


推荐阅读