| この記事の要点 |
|---|
|
エラーの状況
# 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 / Maya)
- 7 Spring Frameworkのアノテーション一覧
- 8 【Spring】@Valueアノテーションとは
- 9 CATALINA_HOME の確認方法 (Linux / Mac)
- 10 【Spring】@Autowiredアノテーションとは
最近更新/作成されたページ
- プロジェクトをTomcatプロジェクトとして認識させる方法 2026-10-07 22:32:50
- MySQLの1366 Incorrect string value|Laravelの文字コード・絵文字エラー 2026-10-07 21:54:03
- curlの証明書ホスト名不一致|旧エラー51・現行60の確認と対処 2026-10-07 21:54:03
- LaravelのMassAssignmentException|fillableの原因と安全な対処 2026-10-07 21:54:03
- Eclipse で Tomcat の起動ログがコンソールに出ない時の確認手順 2026-10-07 21:54:02
- MySQLにおける中央値(Median)の導き方(バージョン8未満) 2026-10-07 13:49:45
- getInputForward 2026-10-07 13:41:15
- JSONから配列に変換 2026-10-07 13:41:15
- ビューから値をモデルに格納しコントローラーで受け取る方法 2026-10-07 13:23:41
- Laravelのテーブル作成と定義変更|マイグレーション・up/down・注意点 2026-10-07 13:23:41
- NumPy 配列に要素を追加する方法 (append / concatenate) 2026-10-07 13:23:41
- MariaDB・MySQLで現在日時を取得する方法|NOW・タイムゾーン・保存型 2026-10-07 13:13:36
- 【django】テンプレートで定数を使用する方法 2026-10-07 13:10:15
- Spring BootにおけるApplication.propertiesの環境依存設定の分割方法 2026-10-07 12:09:35
- Not supported for DML operations【Springエラー】 2026-10-07 11:09:38