Posting JSON via jQuery to Rails

I have an amazingly difficult time when json posts a message from javascript to a rail-supported web service.

Here is the jquery code;

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> var jsonPost = { "email":" dummy@dummy.com ", "token":"63uO6eEfLVBFpnZswzI", "content": "testcontent", "notification_type": "2", "name":"testname", } function callback(data){alert(data)}; jQuery.ajax({ type: "POST", url: 'http://localhost:3000/api/create.json', data: jsonPost, success: callback, dataType: "json", contentType: "application/json", processData: false }); </script> 

On the side of the rails:

 def api_create respond_to do |format| format.json { email = params[:email] token = params[:token] auto_action = params[:auto_action] 

The side of the rails recognizes the request as JSON, but I do not see the data in the request when debugging in rails.

Can someone point me in the right direction?

+4
source share
1 answer

You do not need to convert JSON to string if you do the opposite in rails?

 jQuery.ajax({ type: "POST", url: 'http://localhost:3000/api/create.json', data: JSON.stringify(jsonPost), success: callback, dataType: "json", contentType: "application/json", processData: false }); 
+1
source

All Articles