如何解决登录页面后如何使用异步功能创建新帖子
我需要帮助来创建一个表单,供用户登录并在确认使用令牌登录后创建自己的帖子。本质上,我正在尝试创建一种表单,该表单将提交输入的值以在页面上创建帖子,但仅在用户登录后才显示该表单。
这是我的html:
<div class = "addNewPost">
<div class = "content">
<h3>Create New Post</h3>
<form class="" action="" method="">
<label for="title">Title</label>
<input type="text" id="title" required="">
<!-- <h5>This is a required field</h5> -->
</br>
<label for="description">Description</label>
<textarea id="description" columns="40" rows="5"></textarea>
</br>
<label for="location">Location</label>
<textarea id="location" columns="40" rows="1"></textarea>
</br>
<label for="price">Price</label>
<textarea id="price" columns="40" rows="1"></textarea>
</br>
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1">
<label class="custom-control-label" for="customCheck1">Shipping Available</label>
</div>
<div class="actions">
<button class="create-newpost">
Add
</button>
<button class="action cancel-new-post">
<span class="action-text">Cancel</span>
</button>
</div>
这是我的JS:
const newPost = async post => {
try {
const response = await fetch(`${ API }/posts`,{
method: 'POST',headers: {
'Content-Type': 'application/json','Authorization': `Bearer ${state.token}`
},body: JSON.stringify({post})
});
const addingPost = await response.json();
console.log('addingPost: ',addingPost.data.post);
state.posts.unshift(addingPost.data.post)
} catch (error) {
console.error(error)
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。