登录页面后如何使用异步功能创建新帖子

问题描述

我需要帮助来创建一个表单,供用户登录并在确认使用令牌登录后创建自己的帖子。本质上,我正在尝试创建一种表单,该表单将提交输入的值以在页面上创建帖子,但仅在用户登录后才显示该表单。

这是我的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 (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...