簡體   English   中英

post請求到JSON服務器通過FETCH api刷新頁面

[英]Post request to JSON server through FETCH api refreshes the page

我正在嘗試通過 fetch API 將 POST 請求發送到 JSON 服務器。 Function 通過簡單的按鈕點擊調用(輸入“按鈕”,而不是“提交”)。 當我用 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)
        });

如果您使用 Live Server 擴展,請嘗試禁用它並重試。

檢查您機器上運行的 Json 服務器端口號

附上 html 表單代碼,這樣我們就可以在 oru 本地機器上嘗試重現問題....這有助於我們輕松解決問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM