4

I'm trying to make a POST request to my Sinatra app, but I'm having problems. Essentially I have an input field that on submit does something like this in the JS:

$.post("/", { info: "some_info"});

that is being received by sinatra like this:

post '/' do
 data = JSON.parse(request.body.read)
end

However, in terminal it is saying:

JSON::ParserError - 706: unexpected token at '"info=some_info"':

This means that it is clearly getting the info on the server side, but I'm not sure why it is throwing this error. I've never used AJAX before. I'm not sure either once I get the information how I'm to get the things that I need out of it.

JMax
26.7k12 gold badges74 silver badges89 bronze badges
asked Jul 29, 2011 at 7:17

1 Answer 1

4

When you're sending the request, it's not sent as JSON, but as POST data. This means that you will have access to it on the server side by simply using the params object.

post '/' do
 pp params # outputs {"info"=>"some_info"} in the console
end
answered Jul 29, 2011 at 11:28
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I knew it must not be too complicated, I just couldn't find the answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.