1

I have a 2d array in javascript which I have to send to sinatra and store using datamapper, I've been trying to put them in forms, but its a bit difficult to do it with forms, can I send an array to sinatra and process it?

1 Answer 1

2

You can send it to Sinatra encoding the content as a JSON string and passing it using the body part of a POST request.

Here's how the string should look like.

require 'json'
JSON.dump([[1,2,3], [4,5,6], [7,8,9]])
# => "[[1,2,3],[4,5,6],[7,8,9]]" 

Send the content to Sinatra (using curl or any other HTTP library)

$ curl -d "[[1,2,3],[4,5,6],[7,8,9]]" http://localhost:3000/loader

Then instruct Sinatra to handle the path

post '/loader' do
  json = JSON.load(request.body.read)
  # ... do something with json
end

Note. I haven't tried the code actually, so you might need small changes to make it working. This is just a prototype.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, putting together your answer and @raina77ow 's answer JS/JQUERY: $.post("/save/sources",JSON.stringify(2darray),function(data){console.log(data)}); RUBY: post '/save/sources' do json=JSON.load(request.body.read) json.each{|j| }

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.