0

my application should sent structure via json, that functions works fine:

function send_json()
{
    var formData = form2object('myForm');
    var json_data = JSON.stringify(formData, null, '\t');
    $.post("", json_data);
}

It create json_data and send it via post. But I don't understand how to catch this data on another side, can you help me ? I should take this structure and make some actions with it.

when i send data i got json:

{ "type": "test", "ttl": "1", "amount": "1", "rules": { "rule1": "1", "value1": "4444", "stackcount1": "1", "rule2": "2", "value2": "333", "stackcount2": "2" }}

In the contoller i wrote this:

class AdminController < ApplicationController
  def index
    @json_string = params[:amount]
    if !@json_sting.nil?
      @json_string = JSON.parse(params[:amount])
    end
  end
end

in the view(haml) i wrote:

=@json_string

It doesn't work !

But wireshark shows:

POST /admin/ HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20100101 Firefox/14.0.1
Accept: */*
Accept-Language: ru-ru,ru;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:3000/admin/
Content-Length: 192
Cookie: _GiftSenderTool_session=BAh7BzoQX2NzcmZfdG9rZW4iMTVyNVJCMTBvcGpKSk5OcFN4bW15YVlpRlF1TUNtb1gwZkY0bTRuRHlsNnM9Ig9zZXNzaW9uX2lkIiU4ZjFjNTNjNTY0MTQ1MGExNmFiODcxZGEyYzU5ZTkxMg%3D%3D--f21bf3b96edc3bc551c8de68acc6da17685d58b4
Pragma: no-cache
Cache-Control: no-cache

{
."type": "test",
."ttl": "1",
."amount": "1",
."rules": {
.."rule1": "1",
.."value1": "4444",
.."stackcount1": "1"
.},
."authenticity_token": "n3B6D/Km9zOgOzlosK/OLHyTCDLk3MyCTlTBinfQcnY="
}

It means that i can't get data.

3
  • You probably want to use respond_to with format.json. Read this ASCIICast about controllers. Commented Aug 10, 2012 at 14:24
  • 2
    @mrzasa, sounds like he wants to read json in his Rails app, not output it. Commented Aug 10, 2012 at 14:27
  • I made variable in json format and i sent this json to the same page(doesn't matter, i could sent it to another page). How should I take this json in page ? Some actions, actions which i can do with data, everything. I can put it to database or put to view. Of course I wrote about rails, i set tags. Commented Aug 10, 2012 at 14:33

1 Answer 1

1

you have to catch your JSON object in some rails controller action. If your JSON string looks like this:

{ "user": { "id": 1, "name": "Tom" } }

then do it like this:

require "json"
...
def some_action
  user = JSON.parse(params[:user])
  puts "User id is #{user[:id]}"
  ...
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.