1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails + jQuery]初学者向けAjaxを取り敢えず飛ばしてみたい

Posted at

#課題
初学者が感覚掴むために取り敢えずAjax飛ばしてみたい。

例として以下フォームで入力された文字をサーバに送ってみる。

form.erb
<%= text_field_tag :myname %>
output
<input type="text" name="myname" id="myname">

スクリーンショット 2021-01-24 11.18.12.png

#結論
以下コードで取り敢えず飛ぶ。

ajax.js
$("#myname").change(function(){
  let name = $(this).val();
  $.ajax({
    type: 'GET',
    url: "/api/v1/users",
    data: { name: name },
    dataType: 'json'
    })
  .done(function (response) {
    console.log(response)
  }
  .fail(function (){
  });
})

受け口のRouteやControllerは一般的なRailsの範囲なので割愛します。

#参考情報
Ruby on RailsのAjax処理のおさらい
https://qiita.com/ka215/items/dfa602f1ccc652cf2888

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?