How to use Opal-JQuery AJAX to Post / Get values

219 views Asked by At

I'm attempting to post values to a server using opal-jquery and I'm not having much success. If I attempt a post I don't get anything back. A Get gives me the values in a less than useful string.

Here is the console output from the browser:

[Log] {"values"=>"10:30 AM, 11:30 AM, 12:30 PM, 1:30 PM, 2:30 PM, 3:30 PM"} (opal.min.js, line 7)

Here is the console from the server. First one is the post, second one the get. Post is empty.

{}
{
  "{\"values\""   => ">\"10:30 AM, 11:30 AM, 12:30 PM, 1:30 PM, 2:30 PM, 3:30 PM\"}"
}

Browser Side Code:

  <script type="text/ruby">
Document.ready? do
  puts 'Document ready'

  Element.find('#setTimeButton').on :click do |e|
    e.stop
    e.prevent
    p values = {values: "#{Element.find('#tourtime1').value}, #{Element.find('#tourtime2').value}, #{Element.find('#tourtime3').value}, #{Element.find('#tourtime4').value}, #{Element.find('#tourtime5').value}, #{Element.find('#tourtime6').value}"}
    HTTP.post("/tour-submit-post", payload: values)
    HTTP.get("/tour-submit?#{values}")
  end
end

Server Side Code:

  post "/show-submit-post", response_type: :json do |request|
    mp request.params
    {a: 1, b: 2}
  end

  get "/tour-submit" do |request|
     mp request.params
     {a: 1, b: 2}
  end
1

There are 1 answers

0
Elia Schito On

In the GET you're passing a Hash instance interpolated in the URL string which means that is being converted to a string with .to_s so server side sinatra tries to split the params where it finds an equal sign, which is found in the hash rocket ("=>").

For the POST I suggest to check the browser debug tools to see if the request is sent correctly and then check also if .params is the right method to get POST data out of the request.