简体   繁体   English

发布请求后如何防止重新加载?

[英]How prevent reload after post request?

I am trying to make a post request to the server and do something with the response.我正在尝试向服务器发出发布请求并对响应做一些事情。 Things seem to work on the server-side.事情似乎在服务器端起作用。 The problem is that the page reloads upon completion of the response and I cannot do anything with the response.问题是页面在响应完成后重新加载,我无法对响应做任何事情。

The common suggestions are using button or preventDefault .常见的建议是使用buttonpreventDefault These suggestions do not solve the problem: as you can see below, the input type is button (not submit ) and preventDefault() on the event does not work.这些建议并不能解决问题:正如您在下面看到的,输入类型是button (不是submit )并且 event 上的preventDefault()不起作用。

Does anyone have an idea about what I am missing?有人知道我缺少什么吗?

 <form id="modelCodeForm">
    <label for="codehere"></label>
    <div id="modelAreaDiv">
      <textarea id="modelArea" cols="60" rows="20">
stuff
      </textarea>
      <br>
      <input id="submitUserModel" type="button" value="run on server">
  </div>
  </form>
function initializeUserModel(){
  let model = document.getElementById("modelArea").value;
  fetch('http://localhost:8080/', {
      method: 'post',
      headers: {'Content-Type': 'text/plain'},
      body: model
    })
      .then(response => response.json())
      .then(data => {
        console.log(data);
      }).then(console.log("received!"))     
}  

I got to the bottom of this.我明白了这一点。 It turns out that the problem was due to VS Live Server which was detecting a change in the folder and hot-loading the app.事实证明,问题是由于 VS Live Server 检测到文件夹中的更改并热加载应用程序造成的。 The change was due to the backend, in the same folder, saving a log.更改是由于后端在同一文件夹中保存日志。 Really silly thing to miss...错过真的很傻...

If you want to trigger your function when the form is submitted then you can use the "onsubmit" event listener available on HTML forms.如果您想在提交表单时触发 function,那么您可以使用 HTML forms 上可用的“onsubmit”事件侦听器。

So you would do onsubmit="initializeUserModel(event)" .所以你会做onsubmit="initializeUserModel(event)" You pass it the event object so you can call event.preventDefault() in the function and stop the page from reloading.您将事件 object 传递给它,这样您就可以在 function 中调用event.preventDefault()并停止重新加载页面。

Change your input to type="submit" (or make it a button of type="submit") or the form submission won't be triggered.将输入更改为 type="submit"(或将其设置为 type="submit" 的按钮),否则不会触发表单提交。

    <form id="modelCodeForm" onsubmit="initializeUserModel(event)">
      <label for="codehere"></label>
      <div id="modelAreaDiv">
        <textarea id="modelArea" cols="60" rows="20">Stuff</textarea>
        <br />
        <input id="submitUserModel" type="submit" value="run on server" />
      </div>
    </form>
  function initializeUserModel(event) {
    event.preventDefault();
    let model = document.getElementById("modelArea").value;
    fetch("http://localhost:8080/", {
      method: "post",
      headers: { "Content-Type": "text/plain" },
      body: model,
    })
      .then((response) => response.json())
      .then((data) => {
        console.log(data);
      })
      .then(console.log("received!"));
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM