Problems submitting to Sinatra using jQuery

I am trying to make a POST request to my Sinatra application, but I have problems. Essentially, I have an input field that on submit does something similar in JS:

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

who receives a sinatra like this:

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

However, the terminal says:

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

This means that it is explicitly receiving information on the server side, but I'm not sure why it is throwing this error. I have never used AJAX before. I'm not sure that one day I will receive information on how I should get what I need.

+4
source share
1 answer

When you send a request, it is not sent as JSON, but as POST data. This means that you will have access to it on the server side, simply using the params object.

 post '/' do pp params # outputs {"info"=>"some_info"} in the console end 
+4
source

All Articles