質問編集履歴

1

_ajaxlike.jsを追加しました。

2021/05/21 07:42

投稿

kogjhrf
kogjhrf

スコア0

test CHANGED
File without changes
test CHANGED
@@ -39,3 +39,77 @@
39
39
  require('./ajaxlike.js');
40
40
 
41
41
  ```
42
+
43
+
44
+
45
+ _ajaxlike.js
46
+
47
+ ```
48
+
49
+ $(function () {
50
+
51
+ var like = $('.js-like-toggle');
52
+
53
+ var likeRecipeId;
54
+
55
+
56
+
57
+ like.on('click', function () {
58
+
59
+ var $this = $(this);
60
+
61
+ likeRecipeId = $this.data('recipeid');
62
+
63
+ $.ajax({
64
+
65
+ headers: {
66
+
67
+ 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
68
+
69
+ },
70
+
71
+ url: '/ajaxlike',
72
+
73
+ type: 'POST',
74
+
75
+ data: {
76
+
77
+ 'recipe_id': likeRecipeId
78
+
79
+ },
80
+
81
+ })
82
+
83
+
84
+
85
+ .done(function (data) {
86
+
87
+ $this.toggleClass('loved');
88
+
89
+ $this.next('.likesCount').html(data.RecipeLikesCount);
90
+
91
+
92
+
93
+ })
94
+
95
+
96
+
97
+ .fail(function (data, xhr, err) {
98
+
99
+ console.log('エラー');
100
+
101
+ console.log(err);
102
+
103
+ console.log(xhr);
104
+
105
+ });
106
+
107
+
108
+
109
+ return false;
110
+
111
+ });
112
+
113
+ });
114
+
115
+ ```