我有一個使用 node 和 express 構建的網路應用程式,我想在 ejs 檔案中提交一個表單,但我無法使用,form.submit()因為我想在 URL 中傳遞引數。
表單的 id 是"update",提交按鈕 id 是"btnsubmit",我目前正在嘗試按如下方式發送發布請求:
var form = document.getElementById("update");
document.getElementById("btnsubmit").addEventListener("click", function () {
var lp = "1";
window.location = ("/dbinsertupdateditem?loadpage=" encodeURIComponent(lp));
});
單擊按鈕時,我收到錯誤:Cannot GET /dbinsertupdateditem,因為快速路由需要 POST 請求。是否可以使用 window.location 發出 POST 請求,或者我是否需要采用不同的方法來解決這個問題?
uj5u.com熱心網友回復:
您無法更改 window.location.href 的作業方式。因此,如果路由器希望它是一個帖子,則您需要發布一個表單。如果頁面上有表單,則可以設定操作
var form = document.getElementById("update");
form.action = "/dbinsertupdateditem?loadpage=" encodeURIComponent(lp);
form.submit();
如果您沒有表格,請創建一個
var form = document.createElement("form");
form.method = "POST";
form.action = "/dbinsertupdateditem?loadpage=" encodeURIComponent(lp);
document.body.append(form);
form.submit();
或者可能是最好的答案,更改您的后端以允許 GET
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/357747.html
標籤:javascript 节点.js 表达 http ejs
