回答編集履歴

2 you don't need so many options to make it work.

Hungmi

Hungmi score 15

2018/10/24 12:13  投稿

Hi again, I found something related. If you like, you can check it.  
http://api.jquery.com/jquery.ajax/  
Please check the explanation of "processData" option, which I post it below:  
```HTML  
processData (default: true)  
Type: Boolean  
By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.  
```  
And I don't think you need this option to make it work.  
Just use this code block below, it is enough to make a JSON request:  
```HTML  
   <script type="text/javascript">  
       var data = { 'product_id' : 10, 'user_id' : 12 };  
       $.ajax({  
         url: '/test',  
         type: 'GET',  
         data: data,  
         dataType: 'json'  
       })  
   </script>  
```  
The server is processing as JSON  
```HTML  
Processing by PagesController#test as JSON  
 Parameters: {"product_id"=>"10", "user_id"=>"12"}  
```  
 
---  
 
Hi, I tried your code and it works for me.
I can access params[:product_id] and params[:user_id] in the action.
I am using jQuery 1.11.1 + Rails 5.2.1
Can you provide more info?
```html
   <script type="text/javascript">
       var data = { 'product_id' : 10, 'user_id' : 12 };
       $.ajax({
         url: '/test',
         type: 'GET',
         data: data,
         rocessData: false,
         contentType: false,
         dataType: 'json'
       })
   </script>
```
```html
Started GET "/test?product_id=10&user_id=12" for 127.0.0.1 at 2018-10-23 18:22:54 +0800
Processing by PagesController#test as JSON
 Parameters: {"product_id"=>"10", "user_id"=>"12"}
```
1 Add server logs

Hungmi

Hungmi score 15

2018/10/23 19:31  投稿

Hi, I tried your code and it works for me.
I can access params[:product_id] and params[:user_id] in the action.
I am using jQuery 1.11.1 + Rails 5.2.1
Can you provide more info?
```html
   <script type="text/javascript">
       var data = { 'product_id' : 10, 'user_id' : 12 };
       $.ajax({
         url: '/test',
         type: 'GET',
         data: data,
         rocessData: false,
         contentType: false,
         dataType: 'json'
       })
   </script>
```  
 
```html  
Started GET "/test?product_id=10&user_id=12" for 127.0.0.1 at 2018-10-23 18:22:54 +0800  
Processing by PagesController#test as JSON  
 Parameters: {"product_id"=>"10", "user_id"=>"12"}  
```

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る