| この記事の要点 |
|---|
|
エラーの状況
# Chrome の警告
Form submission canceled because the form is not connected.
# 動作: フォーム送信が実行されない
# サーバへリクエストが行かない
原因と対処
原因 1: form が DOM から削除されている
<form id="myForm" action="/submit" method="post">
<input name="data">
</form>
<script>
const form = document.getElementById("myForm");
// フォームを削除
form.remove();
// その後 submit しようとする
form.submit();
// → Form submission canceled because the form is not connected
</script>
# 修正: 削除前に送信
form.submit();
form.remove(); // 送信後に削除
原因 2: appendChild する前の動的フォーム
// 動的フォームをまだ append していない
const form = document.createElement("form");
form.action = "/api/submit";
form.method = "POST";
const input = document.createElement("input");
input.name = "token";
input.value = "abc123";
form.appendChild(input);
form.submit();
// → Form is not connected (まだ DOM に追加されていない)
// 修正: body に追加してから submit
document.body.appendChild(form);
form.submit();
// 一般的なパターン: 自動送信
const autoSubmit = () => {
const form = document.createElement("form");
form.action = "/payment/external";
form.method = "POST";
const fields = { token: "abc", amount: 1000 };
for (const [key, value] of Object.entries(fields)) {
const input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = value;
form.appendChild(input);
}
document.body.appendChild(form); // ← 必須
form.submit();
};
原因 3: action 属性が空 / 不正
<!-- 空の action -->
<form action="" method="post"> <!-- 自分のページに送信 -->
<form action="javascript:void(0)" method="post"> <!-- JS 無効化 -->
<!-- これらは特殊扱い -->
<!-- 通常のページ URL なら問題なし -->
<!-- 不正な URL -->
<form action="invalid url with spaces"> <!-- ダメ -->
原因 4: SPA でフォーム要素が remove された
// React / Vue 等で
function MyForm() {
const [show, setShow] = useState(true);
return show ? (
<form id="form1" onSubmit={handleSubmit}>...</form>
) : null;
}
// onSubmit ハンドラ内で setShow(false) すると DOM から消える
// その後 e.target.submit() するとエラー
イベントハンドラからの submit
// フォーム送信前にチェック
document.getElementById("myForm").addEventListener("submit", (e) => {
e.preventDefault();
// バリデーション
if (!validate()) {
return;
}
// データ加工
const data = new FormData(e.target);
data.append("timestamp", Date.now());
// submit (e.target がまだ DOM にある)
e.target.submit();
// ❌ こうすると問題:
// e.target.remove(); // ← submit より前に削除
});
fetch / Ajax 推奨
動的フォーム送信なら form.submit() よりも fetch / XHR が制御しやすい:
// fetch でフォームデータ送信
document.getElementById("myForm").addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
try {
const response = await fetch(e.target.action, {
method: e.target.method || "POST",
body: formData,
});
const result = await response.json();
console.log(result);
} catch (error) {
console.error("送信失敗:", error);
}
});
# メリット:
# - フォーム要素の状態に依存しない
# - レスポンスを受け取れる
# - エラーハンドリング可能
# - ページ遷移なし
外部サイト連携での自動送信パターン
// 決済プロバイダ等への自動 POST
function redirectToPayment(token, amount) {
const form = document.createElement("form");
form.action = "https://payment.example.com/checkout";
form.method = "POST";
form.style.display = "none";
const fields = {
token: token,
amount: amount,
return_url: location.origin + "/payment/return"
};
for (const [key, value] of Object.entries(fields)) {
const input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = value;
form.appendChild(input);
}
// body に追加してから submit
document.body.appendChild(form);
// submit の直前に form がまだ DOM にあることを確認
if (form.isConnected) {
form.submit();
} else {
console.error("フォームが DOM から消えた");
}
}
関連: form 要素のチェック
// form.isConnected (DOM に接続されているか、ES6+)
const form = document.getElementById("myForm");
if (form && form.isConnected) {
form.submit();
}
// 古い書き方
if (form && document.body.contains(form)) {
form.submit();
}
// document.contains で確認
if (form && document.contains(form)) {
form.submit();
}
関連エラー
Form submission canceled because the form is not connected: このページFailed to construct \'URL\': action 属性が不正な URLCORS error: 別オリジンへの送信Mixed Content: HTTPS ページから HTTP の form action
関連記事
子ページ
子ページはありません
同階層のページ
- Uncaught TypeError: Illegal invocation
- Form submission canceled because the form is not connected
- Uncaught TypeError: location.href is not a function
- Access to XMLHttpRequest at 'url1' from origin 'url2' has been blocked
- Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')
人気ページ
- 1 Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法
- 2 tomcat の起動 / 停止ログと catalina.log・catalina.out の違い
- 3 JavaScript で base URL を取得する方法|window.location.origin
- 4 YouTube Data API v3 エラー一覧|403・400・404 の原因と対処
- 5 Laravel エラー一覧|500/Blade/DB 接続/ルーティングの代表エラー
- 6 3Dグラフィックスのソフト一覧|Blender・Metasequoiaと用途別の選び方
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- Visual Studio「デザイナーを表示できません」エラーの原因と対処 2026-10-28 12:06:00
- SSH 認証に失敗しました。再試行してください の対処 2026-10-28 12:06:00
- SSH2 秘密鍵の読み込みに失敗しました の原因と対処 2026-10-28 12:06:00
- TeraTerm のよくあるエラーと対処法完全ガイド — Connection refused / 2026-10-28 12:06:00
- Visual Studio エラー一覧|コンパイルエラー・警告の確認方法と対処 2026-10-28 11:39:57
- Whitelabel Error Page の原因と消し方|Spring Boot のエラーページ 2026-10-28 11:39:57
- Eclipseで「サーバーに追加または除去できるリソースがありません。」の原因と対処法 2026-10-28 11:39:57
- SQL WHERE 句完全ガイド 2026-10-28 11:39:57
- SQL JOIN 完全ガイド (INNER/LEFT/RIGHT/FULL OUTER) 2026-10-28 11:39:57
- tomcat の起動 / 停止ログと catalina.log・catalina.out の違い 2026-10-28 10:24:23
- Tomcat の起動確認方法 5 つ|localhost:8080・ポート・プロセス・ログ 2026-10-28 10:24:23
- Oracle 19c COMMITの使い方: 暗黙コミット・自動確定・応答不明時の確認 2026-10-28 09:59:09
- Oracle 19c DELETE文|削除対象の確認・COMMIT前の判断・ROLLBACKの条件 2026-10-28 09:59:09
- Oracle 19c INSERT文|INSERT ALL・SELECT・RETURNINGとMERGEの使い分け 2026-10-28 09:59:09
- Oracle 19cのトランザクション制御|COMMIT・ROLLBACK・分離レベルと接続設定 2026-10-28 09:59:09