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.
1 Answer 1
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