简体   繁体   中英

Cancel redirect after POST

I'm doing a js file in which I need to post a defined url. The issue is that I can not occupy because this block jquery javascript code anger in each of the user sites. What I need is to prevent post after a redirect to the same URL that was made post is made.

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(); 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.

You can't do that. You might need to use AJAX if you don't want the page to refresh.

If you do form.submit() then the page will refresh. When you do event.preventDefault , that means the element should not perform its default action upon receiving the event, thus nothing will be submited.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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