「jQuery」$.postでpostリクエストを送信する
環境
jQuery 3.6.0
書式
$.post(URL [,<送信データ> ,<コールバック関数>])
引数
URL POSTリクエストを送信先URL
送信データ 送信するデータ(key,valueのオブジェクト型)
コールバック関数 通信が成功した時に呼ばれる関数
$.post()を使用してPOSTリクエストを送信します。
使用例
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>POSTリクエストを送信するサンプル</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function(){
$('#btnreg').click(function(){
$.post("confirm.php",
{"depcode":"101", "city":"tokyo"},
function(res){
console.log(res);
}
);
});
});
</script>
</head>
<body>
<input type="button" id="btnreg" value="送信">
</body>
</html>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>POSTリクエストを送信するサンプル</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(function(){
$('#btnreg').click(function(){
$.post("confirm.php",
{"depcode":"101", "city":"tokyo"},
function(res){
console.log(res);
}
);
});
});
</script>
</head>
<body>
<input type="button" id="btnreg" value="送信">
</body>
</html>
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>POSTリクエストを送信するサンプル</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script> $(function(){ $('#btnreg').click(function(){ $.post("confirm.php", {"depcode":"101", "city":"tokyo"}, function(res){ console.log(res); } ); }); }); </script> </head> <body> <input type="button" id="btnreg" value="送信"> </body> </html>
結果
「送信」ボタンを押すと、confirm.phpにPOSTデータを送信し、結果をコンソールに出力します。