简体   繁体   English

如何从表单提交中捕获 GET 请求 url

[英]How to catch GET request url from form submit

I have a quite complicated HTML form.我有一个相当复杂的 HTML 表单。 This form sends a GET request to the server.此表单向服务器发送 GET 请求。 On submit event I would like to catch this GET URL and send it like an ajax request.在提交事件时,我想捕获这个 GET URL 并像 ajax 请求一样发送它。 But don't know how to get this submit URL.但不知道如何获得这个提交网址。

I would like to have it in pure JavaScript and not with jQuery.我想使用纯 JavaScript 而不是 jQuery。 I tried to do it via:我试图通过以下方式做到这一点:

addEventlistener('beforeunload', function(e) {
    e.preventDefault();
    var getUrl = location.href;
});

The problem with this code is that it is not triggered by form submit, and I don't know if location.href is a good idea.这段代码的问题是不是表单提交触发的,不知道location.href是不是个好主意。

How could it be done?怎么可能呢?

Use an event listener on the form's submit event.在表单的submit事件上使用事件侦听器。

document.querySelector("#yourform").addEventListener('submit', function(e) {
  e.preventDefault(); // block the default GET request
  let url = this.action;
  // rest of code for sending AJAX request to url
});

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

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