Javascript 如何在不刷新页面的情况下查看显示的数据?(取回Post请求)

Javascript 如何在不刷新页面的情况下查看显示的数据?(取回Post请求),javascript,json,fetch,Javascript,Json,Fetch,我的fetch post请求正在成功地将数据发布到数据库(具体来说,我使用的是PostGresSQL、ElephantSQL),但是新数据仅在我刷新页面后显示。如何立即将其呈现到页面?另外,在我的res=>res.json()中,我还得到一个错误:error:SyntaxError:json输入的意外结束。我不知道这是为什么,当我在将“res”转换为json之前,在控制台上记录“res”时,我得到了一种“basic”类型。下面是我的post功能的代码 const postMessage

我的fetch post请求正在成功地将数据发布到数据库(具体来说,我使用的是PostGresSQL、ElephantSQL),但是新数据仅在我刷新页面后显示。如何立即将其呈现到页面?另外,在我的res=>res.json()中,我还得到一个错误:error:SyntaxError:json输入的意外结束。我不知道这是为什么,当我在将“res”转换为json之前,在控制台上记录“res”时,我得到了一种“basic”类型。下面是我的post功能的代码

    const postMessage = () => {

  let password;
  const getInputValue = () => {
    password = document.getElementById('pass').value;
  };
  
  let message;
  const getMessageValue = () => {
    message = document.getElementById('desc').value;
  };

  getInputValue();
  getMessageValue();

  //declare a object variable that has all properties you want to send (password, message, time)
  const obj = {
    password: password,
    message: message,
    created_at: new Date().toLocaleDateString()
  };

  fetch('/messages', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json; charset=UTF-8'
    },
    body: JSON.stringify(obj),
  })
    .then(res => res.json())
    .then((data) => {
      console.log(data)
      // const list = addMessage.getElementsByTagName('li');
      // console.log(list)
      // list.innerText = `${message}`;
    
    })
    .catch((error) => console.log('Error: ', error));

  getMessage(messageDisplay);  
};

addMessage.addEventListener('click', postMessage);
这是我在字符串化之前console.log(res)时看到的。

你能粘贴console.log(res)吗?在你将其字符串化之前,它现在应该在我的帖子上。你能分享错误截图吗?很难说到底是什么问题。如果您可以提供某个地方的fetch响应(在这里或pastebin链接),那就太好了