teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

タイトルの変更

2019/10/22 14:25

投稿

nanase21
nanase21

スコア144

title CHANGED
@@ -1,1 +1,1 @@
1
- POSTしたXMLHttpRequest中身を取得する方法
1
+ Ajaxdataについて
body CHANGED
File without changes

4

質問内容の変更

2019/10/22 14:25

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -1,48 +1,29 @@
1
- # 実現したいこと
1
+ ```html
2
- ajaxでPOSTしたxhrのquery パラメーターを取得する方法を知りたい。
3
- サンプルに``like[answer_id]: 1``の中身を取得する方法をご教示いただけると幸いです。
2
+ <button class="border-0" onclick="onclick="Post_like("2", "1", 0, "2")">
4
-
5
3
  ```
6
- Request URL: http://localhost:3000/likes?like%5Banswer_id%5D=1&like%5Bstatus%5D=1&like%5Buser_id%5D=2&question=1
7
- Request Method: POST
8
- Status Code: 204 No Content (from ServiceWorker)
9
- Referrer Policy: strict-origin-when-cross-origin
10
- Cache-Control: no-cache
11
- Referrer-Policy: strict-origin-when-cross-origin
12
- X-Content-Type-Options: nosniff
13
- X-Download-Options: noopen
14
- X-Frame-Options: SAMEORIGIN
15
- X-Permitted-Cross-Domain-Policies: none
16
- X-Request-Id: 51b637b8-80d1-423d-a421-2dd71e77b340
17
- X-Runtime: 0.030867
18
- X-XSS-Protection: 1; mode=block
19
- Provisional headers are shown
20
- Accept: */*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
21
- Origin: http://localhost:3000
22
- Referer: http://localhost:3000/questions/1?no_count=1
23
- Sec-Fetch-Mode: cors
24
- 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
25
- X-CSRF-Token:
26
- X-Requested-With: XMLHttpRequest
27
- like[answer_id]: 1
28
- like[status]: 1
29
- like[user_id]: 2
30
- question: 1
31
- ```
32
4
 
33
- rails slim
34
- ```ruby
5
+ ```js
6
+ javascript:
7
+ function Post_like(user_id, answer_id, status, hoge) {
8
+ alert('user_id:'+user_id+'answer_id:'+answer_id+'status:'+status)
9
+ $.ajax({
10
+ url: "/likes",
11
+ type: "POST",
12
+ data: {user_id: user_id, answer_id: answer_id, status: status, hoge},
13
+ // hogeだけ、likeにネストしないようにしたい。下記の書き方であってるかについて知りたい
35
- = link_to likes_path(like: {user_id: current_user.id, answer_id: a, status: 0}, question: @question), method: :post, remote: true do
14
+ // data: {like {user_id: user_id, answer_id: answer_id, status: status} hoge}
15
+ dataType: "json",
16
+ success: function (data) {
17
+ console.log(data[0].text);
18
+ },
19
+ error: function (data) {
20
+ console.log("error");
21
+ }
22
+ });
23
+ }
36
24
  ```
37
25
 
38
- ```ruby
39
- def create
26
+ # 実現したいこと
27
+ 回答しようとしてくれた方には申し訳ないのですが、元の質問の内容だとあまりにも回答しずらいと思ったのでピポットしました。
40
- @like = Like.new(create_params)
28
+ hogeだけ、likeにネストしないようにしたい。下記の書き方であってるかについて知りたい
41
- @like.save!
42
- respond_to do |format|
43
- format.html
44
- format.json { render json: @like }
29
+ data: {like {user_id: user_id, answer_id: answer_id, status: status} hoge}
45
- format.xml { render xml: @like }
46
- end
47
- end
48
- ```

3

追記の修正

2019/10/22 14:22

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -34,7 +34,6 @@
34
34
  ```ruby
35
35
  = link_to likes_path(like: {user_id: current_user.id, answer_id: a, status: 0}, question: @question), method: :post, remote: true do
36
36
  ```
37
- remote trueで非同期でPOSTしています。
38
37
 
39
38
  ```ruby
40
39
  def create

2

追記の修正

2019/10/22 13:24

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -28,4 +28,22 @@
28
28
  like[status]: 1
29
29
  like[user_id]: 2
30
30
  question: 1
31
+ ```
32
+
33
+ rails slim
34
+ ```ruby
35
+ = link_to likes_path(like: {user_id: current_user.id, answer_id: a, status: 0}, question: @question), method: :post, remote: true do
36
+ ```
37
+ remote trueで非同期でPOSTしています。
38
+
39
+ ```ruby
40
+ def create
41
+ @like = Like.new(create_params)
42
+ @like.save!
43
+ respond_to do |format|
44
+ format.html
45
+ format.json { render json: @like }
46
+ format.xml { render xml: @like }
47
+ end
48
+ end
31
49
  ```

1

追記

2019/10/22 13:24

投稿

nanase21
nanase21

スコア144

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,31 @@
1
1
  # 実現したいこと
2
2
  ajaxでPOSTしたxhrのquery パラメーターを取得する方法を知りたい。
3
+ サンプルに``like[answer_id]: 1``の中身を取得する方法をご教示いただけると幸いです。
4
+
5
+ ```
6
+ Request URL: http://localhost:3000/likes?like%5Banswer_id%5D=1&like%5Bstatus%5D=1&like%5Buser_id%5D=2&question=1
3
- 下記のスクショは実際のxhrの中身です。
7
+ Request Method: POST
8
+ Status Code: 204 No Content (from ServiceWorker)
9
+ Referrer Policy: strict-origin-when-cross-origin
10
+ Cache-Control: no-cache
11
+ Referrer-Policy: strict-origin-when-cross-origin
12
+ X-Content-Type-Options: nosniff
13
+ X-Download-Options: noopen
14
+ X-Frame-Options: SAMEORIGIN
15
+ X-Permitted-Cross-Domain-Policies: none
4
- ![xhr](d0ec12757b6b19bf06fa160ef812661b.png)
16
+ X-Request-Id: 51b637b8-80d1-423d-a421-2dd71e77b340
17
+ X-Runtime: 0.030867
18
+ X-XSS-Protection: 1; mode=block
19
+ Provisional headers are shown
20
+ Accept: */*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
21
+ Origin: http://localhost:3000
22
+ Referer: http://localhost:3000/questions/1?no_count=1
23
+ Sec-Fetch-Mode: cors
24
+ 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
25
+ X-CSRF-Token:
26
+ X-Requested-With: XMLHttpRequest
27
+ like[answer_id]: 1
28
+ like[status]: 1
29
+ like[user_id]: 2
30
+ question: 1
31
+ ```