ajax - 为什么在编辑项目后我的数据会附加到我的 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>
解决方案
如果您对表单使用 onSubmit 处理程序,这是默认行为。如果这不是您要查找的内容,请不要在表单上使用 onSubmit 处理程序,而是对提交按钮使用 onClick 处理程序来调用 updateRecipe 功能。然后不修改 Url。
推荐阅读
- javascript - 在 JavaScript 中使用 JSON 字符串化结果
- firebase - 仅按用户在 Firebase/Firestore 上查看某些集合
- angular - AmCharts4 - 将 y = 0 轴与多个 y 轴同步
- html - 改变css箭头的方向
- javascript - 如何使用 Javascript 或 Jquery 在页面加载时关闭手风琴(Wordpress)?
- angular - 有没有办法为 Angular 使用的 HTML 创建动态引用?
- json - 将json数据插入elasticsearch的最佳和最有效的方法是什么
- active-directory - 通过组策略将 Adobe 设置为默认程序?
- airflow - 气流警告:第 1 行的“execution_date”列的日期时间值不正确
- swift - PDFPage 的“setBounds”不裁剪应用于 pdfpage 的注释