jQuery3 Ajax通信のサンプルコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // ex) カテゴリ追加 $(function(){ $.ajax({ url: '/add', // リクエスト送信先URL type: 'POST', // get or postメソッド timeout: 2000, // タイムアウト時間をミリ秒で設定 cache: false, // キャッシュ無効 data: {'category_name': category_name}, // サーバーに送信する値 dataType: 'json' // サーバーから返されるデータ型 }).done(function (response, textStatus, jqXHR) { // サーバー側[php] echo json_encode(['status' => true or false]); if (response.status) { // 追加成功 alert('success.'); } else { // 追加失敗 alert('error.'); } }).fail(function (jqXHR, textStatus, errorThrown) { // 通信失敗 alert('error.'); }); }); |