簡體   English   中英

Chrome中的Javascript window.location.href在POST方法后不起作用

[英]Javascript window.location.href in chrome doesn't work after POST method

我已經更改了html密碼表單。 當用戶提交該表單時,我寫了另一個<p> </p>元素來告訴用戶更改了密碼或沒有更改密碼,然后將鏈接添加到“登錄”頁面。 鏈接到“登錄”和“取消”按鈕的功能相同-重定向到登錄頁面。 但是,在使用POST方法后,當我單擊“取消/重定向”以登錄按鈕時,屏幕僅保持加載狀態,而從未真正將您重定向到那里。 如果我再次單擊“提交”按鈕,它將再次發送POST請求,因此無論我發送了多少個請求,此按鈕都可以正常工作。 重定向有什么問題? 我似乎無法弄清楚。 我在Firefox中進行了檢查,似乎可以正常工作。 我的代碼如下:

 document.getElementById("btn_cancel").onclick = function(event) { window.location.href = "/login"; }; var token = window.location.href.substring(window.location.href.lastIndexOf("/") + 1); function validate() { var responseText = document.getElementById('error_id'); var password = document.forms["reset-pasword"]["new_password"].value; var confirmPassword = document.forms["reset-pasword"]["repeat_password"].value; if (password !== confirmPassword) { error = "Passwords do not match"; responseText.innerHTML = error; responseText.className = "error_text"; return false; } return true; } document.getElementById("btn_change").onclick = function(event) { event.preventDefault(); var responseText = document.getElementById('error_id'); if (validate() != true) return; var password = document.getElementById("new_password").value; var request = { token: token, password: password }; var xhr = new XMLHttpRequest(); xhr.open('POST', '/update', true); xhr.setRequestHeader('Content-type', 'application/json'); xhr.onload = (res) => { response = res['target']['response']; if (response) { response = JSON.parse(response); responseText.innerHTML = response.message; responseText.className = "error_text"; } else { responseText.innerHTML = "Password changed succesfully. <a href=\\"/login\\">Login</a>"; responseText.className = "success_text"; } }; xhr.send(JSON.stringify(request)); }; 
 <body onload="document.getElementById('reset-pasword').focus();"> <div class="box" id="change_password_panel"> <form name="reset-pasword" id="reset-pasword"> <label for="password">New password</label> <input type="password" placeholder="New Password" id="new_password" name="new_password" required /> <label for="password">Repeat new password</label> <input type="password" placeholder="Repeat Password" id="repeat_password" name="repeat_password" required /> <div style="width: 100%; display: inline-block;margin-top: 10px;"> <div style="float: left;"><button id="btn_change" class="button">Change password</button> </div> <div style="float: right;"><button id="btn_cancel" type="button" class="button">Cancel</button></div> </div> <p id="error_id"></p> </form> </div> </body> 

另外,如果我先單擊“ Cancel按鈕,則在單擊“提交”之前,重定向工作正常。

如果我把window.location.href = "/login"; xhr.onload中的if和else語句中也不起作用。 所以問題可能出在POST方法上? 我真的迷失了這個..

當我在提交表單之前單擊“取消”時,這是網絡: 在此處輸入圖片說明

這是在之后: 在此處輸入圖片說明

它甚至都沒有“登錄” ...我也嘗試過

 document.getElementById("btn_cancel").onclick = function (event) { event.preventDefault(); fetch('/login/default.html') .then(window.location.href = "default.html"); }; 

但似乎它只在window.location.href而從不外出

我曾經有過類似的問題,但仍然不知道出了什么問題,但是我解決了這個問題,在按鈕上添加了<a>標記添加事件。

document.getElementById("btn_cancel").innerHTML = "<a href='/login'>Cancel</a>";

你能試一下嗎

  setTimeout(function(){document.location.href = "/login;"},500);

要么

  window.location.assign("/login");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM