<div>
<form id="blogForm">
<table>
<tr>
<td>博客标题:</td>
<td><input type="text" name="title" value=""></td>
</tr>
<tr>
<td>作者:</td>
<td><input type="text" name="author" value=""></td>
</tr>
<tr>
<td>分类:</td>
<td>
<select name="classify" id="">
<option value="1">后端开发</option>
<option value="2">前端开发</option>
<option value="3">软件工程</option>
<option value="4">数据库</option>
</select>
</td>
</tr>
<tr>
<td><input id="btnSave" type="button" value="保存"></td>
<td><input id="btnCancel" type="reset" value="取消"></td>
</tr>
</table>
</form>
</div>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
document.getElementById("btnCancel").addEventListener("click",() => {
location.href = "./index.html";
})
<p>document.getElementById(&quot;btnSave&quot;).addEventListener(&quot;click&quot;,() =&gt; {
const form = document.getElementById(&quot;blogForm&quot;);
const formData = new FormData(form);</p>
<p>axios.post(&quot;http://localhost:3000/api/blogs/add&quot;, formData)
.then(response =&gt; {
console.log(response.data); // 在控制台打印出后端返回的数据
// 在前端展示新增的数据,比如更新页面或者跳转到其他页面
})
.catch(error =&gt; {
console.error(error);
});
});
</script></p>

原文地址: https://www.cveoy.top/t/topic/pK4d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录