简体   繁体   English

开机自检后取消重定向

[英]Cancel redirect after POST

I'm doing a js file in which I need to post a defined url. 我正在做一个js文件,我需要在其中发布定义的网址。 The issue is that I can not occupy because this block jquery javascript code anger in each of the user sites. 问题是我无法占据,因为此jquery javascript代码会激怒每个用户站点。 What I need is to prevent post after a redirect to the same URL that was made post is made. 我需要做的是防止在重定向到发布相同的URL后发布。

function post(path, parameters) {
var event = window.event;
var form = document.createElement("form");
form.setAttribute('method',"post");
form.setAttribute('action',path);
for (var key in parameters) {
    if (parameters.hasOwnProperty(key)) {
       console.log(key+parameters[key]);
        var valor = document.createElement("input");
        valor.setAttribute('type',"hidden");
        valor.setAttribute('name',key);
        valor.setAttribute('value',parameters[key]);
        form.appendChild(valor);
    }
}
console.log(form);
form.submit();
event.preventDefault();
}

This event.preventDefault(); 这个event.preventDefault(); not working :( The reason it can not take jquery is only because as this block goes where the users can not rely or not they occupy jquery for this to work. 不起作用:(之所以不能使用jquery,仅是因为当此块进入用户无法依赖或无法占用jquery才能使其工作的地方时,才可以使用jquery。

You can't do that. 你不能那样做。 You might need to use AJAX if you don't want the page to refresh. 如果您不希望刷新页面,则可能需要使用AJAX。

If you do form.submit() then the page will refresh. 如果执行form.submit()则页面将刷新。 When you do event.preventDefault , that means the element should not perform its default action upon receiving the event, thus nothing will be submited. 当您执行event.preventDefault ,这意味着该元素不应在收到事件后执行其默认操作,因此将不提交任何内容。

Also, if you want to prevent the form submission, the correct way would be to add a listener to it like so: 另外,如果您要阻止表单提交,则正确的方法是向其添加侦听器,如下所示:

form.addEventListener('submit', function(evt){
  evt.preventDefault();
});

But then, nothing will happen. 但是然后,什么也不会发生。

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

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