通过FETCH api向JSON服务器发布请求会刷新页面

时间:2020-04-15 09:55:03

标签: javascript post fetch-api json-server

我正在尝试通过获取API将POST请求发送到JSON服务器。单击简单的按钮即可调用该函数(键入“ button”,而不是“ submit”)。当我用GET请求替换POST请求时,一切按预期进行,但使用POST时出现问题。在JSON服务器实体上创建请求传递,但在每次请求后都会刷新页面。另外,我没有来自JSON服务器的响应,谷歌浏览器表示“无法加载响应数据”。 我在哪里犯错了?

  const comment = {
      text: "test comment",
      article_id: 3
  };
  console.log(JSON.stringify(comment));
    const options = {
        method: 'post',
        headers: {
            'Content-Type': 'application/json'
        },
        body: JSON.stringify(comment)
    }
    fetch(`${URL_COMMENTS}`, options)
        .then(response => { return response.json() })
        .then(data => {
            console.log(data)
        });

0 个答案:

没有答案