我正在開發 php MVC 專案。
我在后臺 JavaScript 中創建了一個組態檔編輯頁面,If并且Else兩個代碼都在執行。組態檔編輯成功,但其他代碼作業,并顯示錯誤“抱歉,此內容現在不可用”。
為什么這個else條件有效?
- 保存在本地檔案中并運行比它作業:-
幫我!謝謝!!
uj5u.com熱心網友回復:
onreadystatechange該鏈接中提到的所有狀態都會觸發事件。在您的情況下,您將需要檢查狀態何時相等4(即,當操作完成并將回應發送回瀏覽器時)。因此,您可以修改代碼塊,如下所示:xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if(xhr.status == 200){ var json = JSON.parse(xhr.responseText); if (json.Status == "Ok") { window.location.href = "Profile"; } else { document.querySelector("#Profile_Edit_Msg").classList.remove("active_success"); document.querySelector("#Profile_Edit_Msg").classList.add("active_denger"); document.querySelector("#Profile_Edit_Msg").innerHTML = json.Message; } }else{ document.querySelector("#Profile_Edit_Msg").classList.add("active_denger"); document.querySelector("#Profile_Edit_Msg").innerHTML = "Sorry, this content isn't available right now"; // this message show } } };uj5u.com熱心網友回復:
試試這個
if (xhr.readyState != 4 && xhr.status != 200) { document.querySelector("#Profile_Edit_Msg").classList.add("active_denger"); document.querySelector("#Profile_Edit_Msg").innerHTML = "Sorry, this content isn't available right now"; // this message show }else if (xhr.readyState == 4 && xhr.status == 200) { var json = JSON.parse(xhr.responseText); if (json.Status == "Ok") { window.location.href = "Profile"; // it also work } else { document.querySelector("#Profile_Edit_Msg").classList.remove("active_success"); document.querySelector("#Profile_Edit_Msg").classList.add("active_denger"); document.querySelector("#Profile_Edit_Msg").innerHTML = json.Message; } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/489793.html標籤:javascript php html
上一篇:單擊事件未正確回傳正確的元素
