質問編集履歴
5
タイトルの変更
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
Ajaxのdataについて
|
test
CHANGED
File without changes
|
4
質問内容の変更
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,95 +1,57 @@
|
|
1
|
-
|
1
|
+
```html
|
2
2
|
|
3
|
-
ajaxでPOSTしたxhrのquery パラメーターを取得する方法を知りたい。
|
4
|
-
|
5
|
-
サンプルに``like[answer_id]: 1``の中身を取得する方法をご教示いただけると幸いです。
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
```
|
10
|
-
|
11
|
-
Request URL: http://localhost:3000/likes?like%5Banswer_id%5D=1&like%5Bstatus%5D=1&like%5Buser_id%5D=2&question=1
|
12
|
-
|
13
|
-
Request Method: POST
|
14
|
-
|
15
|
-
Status Code: 204 No Content (from ServiceWorker)
|
16
|
-
|
17
|
-
Referrer Policy: strict-origin-when-cross-origin
|
18
|
-
|
19
|
-
Cache-Control: no-cache
|
20
|
-
|
21
|
-
Referrer-Policy: strict-origin-when-cross-origin
|
22
|
-
|
23
|
-
X-Content-Type-Options: nosniff
|
24
|
-
|
25
|
-
X-Download-Options: noopen
|
26
|
-
|
27
|
-
X-Frame-Options: SAMEORIGIN
|
28
|
-
|
29
|
-
X-Permitted-Cross-Domain-Policies: none
|
30
|
-
|
31
|
-
X-Request-Id: 51b637b8-80d1-423d-a421-2dd71e77b340
|
32
|
-
|
33
|
-
X-Runtime: 0.030867
|
34
|
-
|
35
|
-
X-XSS-Protection: 1; mode=block
|
36
|
-
|
37
|
-
Provisional headers are shown
|
38
|
-
|
39
|
-
|
3
|
+
<button class="border-0" onclick="onclick="Post_like("2", "1", 0, "2")">
|
40
|
-
|
41
|
-
Origin: http://localhost:3000
|
42
|
-
|
43
|
-
Referer: http://localhost:3000/questions/1?no_count=1
|
44
|
-
|
45
|
-
Sec-Fetch-Mode: cors
|
46
|
-
|
47
|
-
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
|
48
|
-
|
49
|
-
X-CSRF-Token:
|
50
|
-
|
51
|
-
X-Requested-With: XMLHttpRequest
|
52
|
-
|
53
|
-
like[answer_id]: 1
|
54
|
-
|
55
|
-
like[status]: 1
|
56
|
-
|
57
|
-
like[user_id]: 2
|
58
|
-
|
59
|
-
question: 1
|
60
4
|
|
61
5
|
```
|
62
6
|
|
63
7
|
|
64
8
|
|
65
|
-
|
9
|
+
```js
|
66
10
|
|
67
|
-
|
11
|
+
javascript:
|
68
12
|
|
13
|
+
function Post_like(user_id, answer_id, status, hoge) {
|
14
|
+
|
15
|
+
alert('user_id:'+user_id+'answer_id:'+answer_id+'status:'+status)
|
16
|
+
|
17
|
+
$.ajax({
|
18
|
+
|
19
|
+
url: "/likes",
|
20
|
+
|
21
|
+
type: "POST",
|
22
|
+
|
23
|
+
data: {user_id: user_id, answer_id: answer_id, status: status, hoge},
|
24
|
+
|
25
|
+
// hogeだけ、likeにネストしないようにしたい。下記の書き方であってるかについて知りたい
|
26
|
+
|
69
|
-
|
27
|
+
// data: {like {user_id: user_id, answer_id: answer_id, status: status} hoge}
|
28
|
+
|
29
|
+
dataType: "json",
|
30
|
+
|
31
|
+
success: function (data) {
|
32
|
+
|
33
|
+
console.log(data[0].text);
|
34
|
+
|
35
|
+
},
|
36
|
+
|
37
|
+
error: function (data) {
|
38
|
+
|
39
|
+
console.log("error");
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
});
|
44
|
+
|
45
|
+
}
|
70
46
|
|
71
47
|
```
|
72
48
|
|
73
49
|
|
74
50
|
|
75
|
-
|
51
|
+
# 実現したいこと
|
76
52
|
|
77
|
-
|
53
|
+
回答しようとしてくれた方には申し訳ないのですが、元の質問の内容だとあまりにも回答しずらいと思ったのでピポットしました。
|
78
54
|
|
79
|
-
|
55
|
+
hogeだけ、likeにネストしないようにしたい。下記の書き方であってるかについて知りたい
|
80
56
|
|
81
|
-
@like.save!
|
82
|
-
|
83
|
-
respond_to do |format|
|
84
|
-
|
85
|
-
format.html
|
86
|
-
|
87
|
-
|
57
|
+
data: {like {user_id: user_id, answer_id: answer_id, status: status} hoge}
|
88
|
-
|
89
|
-
format.xml { render xml: @like }
|
90
|
-
|
91
|
-
end
|
92
|
-
|
93
|
-
end
|
94
|
-
|
95
|
-
```
|
3
追記の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -70,8 +70,6 @@
|
|
70
70
|
|
71
71
|
```
|
72
72
|
|
73
|
-
remote trueで非同期でPOSTしています。
|
74
|
-
|
75
73
|
|
76
74
|
|
77
75
|
```ruby
|
2
追記の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -59,3 +59,39 @@
|
|
59
59
|
question: 1
|
60
60
|
|
61
61
|
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
rails slim
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
|
69
|
+
= link_to likes_path(like: {user_id: current_user.id, answer_id: a, status: 0}, question: @question), method: :post, remote: true do
|
70
|
+
|
71
|
+
```
|
72
|
+
|
73
|
+
remote trueで非同期でPOSTしています。
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
|
79
|
+
def create
|
80
|
+
|
81
|
+
@like = Like.new(create_params)
|
82
|
+
|
83
|
+
@like.save!
|
84
|
+
|
85
|
+
respond_to do |format|
|
86
|
+
|
87
|
+
format.html
|
88
|
+
|
89
|
+
format.json { render json: @like }
|
90
|
+
|
91
|
+
format.xml { render xml: @like }
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
```
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,6 +2,60 @@
|
|
2
2
|
|
3
3
|
ajaxでPOSTしたxhrのquery パラメーターを取得する方法を知りたい。
|
4
4
|
|
5
|
-
|
5
|
+
サンプルに``like[answer_id]: 1``の中身を取得する方法をご教示いただけると幸いです。
|
6
6
|
|
7
|
+
|
8
|
+
|
9
|
+
```
|
10
|
+
|
11
|
+
Request URL: http://localhost:3000/likes?like%5Banswer_id%5D=1&like%5Bstatus%5D=1&like%5Buser_id%5D=2&question=1
|
12
|
+
|
13
|
+
Request Method: POST
|
14
|
+
|
15
|
+
Status Code: 204 No Content (from ServiceWorker)
|
16
|
+
|
17
|
+
Referrer Policy: strict-origin-when-cross-origin
|
18
|
+
|
19
|
+
Cache-Control: no-cache
|
20
|
+
|
21
|
+
Referrer-Policy: strict-origin-when-cross-origin
|
22
|
+
|
23
|
+
X-Content-Type-Options: nosniff
|
24
|
+
|
25
|
+
X-Download-Options: noopen
|
26
|
+
|
27
|
+
X-Frame-Options: SAMEORIGIN
|
28
|
+
|
29
|
+
X-Permitted-Cross-Domain-Policies: none
|
30
|
+
|
7
|
-
|
31
|
+
X-Request-Id: 51b637b8-80d1-423d-a421-2dd71e77b340
|
32
|
+
|
33
|
+
X-Runtime: 0.030867
|
34
|
+
|
35
|
+
X-XSS-Protection: 1; mode=block
|
36
|
+
|
37
|
+
Provisional headers are shown
|
38
|
+
|
39
|
+
Accept: */*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
|
40
|
+
|
41
|
+
Origin: http://localhost:3000
|
42
|
+
|
43
|
+
Referer: http://localhost:3000/questions/1?no_count=1
|
44
|
+
|
45
|
+
Sec-Fetch-Mode: cors
|
46
|
+
|
47
|
+
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36
|
48
|
+
|
49
|
+
X-CSRF-Token:
|
50
|
+
|
51
|
+
X-Requested-With: XMLHttpRequest
|
52
|
+
|
53
|
+
like[answer_id]: 1
|
54
|
+
|
55
|
+
like[status]: 1
|
56
|
+
|
57
|
+
like[user_id]: 2
|
58
|
+
|
59
|
+
question: 1
|
60
|
+
|
61
|
+
```
|