如何在localhost中获取post方法?

时间:2020-08-23 02:17:06

标签: javascript django fetch fetch-api

我有索引路由,我有一个带有textarea字段的表单,我想在提交该表单时发送一个发布请求,而无需重新加载页面,所以我正在做:

document.querySelector('form').onsubmit = (event) => {


  fetch("https://jsonplaceholder.typicode.com/posts", {
    method: 'POST',
    body: JSON.stringify({
      body: document.querySelector('#new_message').value
    }),
    headers: {
      "Content-type": "application/json; charset=UTF-8",
      "X-CSRFToken": getCookie('csrftoken')
    }
  })
  .then(response => response.json())
  .then(result => {
      // Print result
      console.log(result);

  });
  event.preventDefault();

}

此代码可以正常工作。但是,如果要在我的索引localhost fetch("/")中发出发布请求,则此操作无效。 console.log不会打印任何内容。另外,使用获取JSONPlaceholder的代码,它不会在我的服务器上返回任何POST请求。如果我只是更改"/"的网址,那将根本行不通。有什么想法吗?

0 个答案:

没有答案
相关问题