Question

Site built on Sinatra, and got next code in actions:

post '/registration/sms/:number' do
  code = Random.rand(1111..9999).to_s
  phone = params[:phone].to_s
  HTTParty.get('http://sms.ru/sms/send?api_id=' + api_id +'&to=' + phone + '&text=' + code)
  # check code
end

With this view:

%form{:method => "post"}
  %div.form-group
    %label Phone number
    %input#phone.form-control{:name => "phone", :placeholder => "7 950 123 45 67"}
  %button#sendsms.btn.btn-default{"data-toggle" => "modal", "data-target" => "#myModal"} Send

And this JS:

$("#sendsms").click(function(){
  var phone = $("#phone").val();
  $.ajax({
    url: "/registration/sms",
    data: {"phone": phone},
    type: "post"
  })
})

I thought this JS should send post request to action, which creates new request to sms.ru wich sends sms. But it doesn't works, and I can't figure out why.

Thank you in advance for any help, especially for implementation without JS.

Was it helpful?

Solution

Your sinatra code expects /registration/sms/:number, but your ajax sends to /registration/sms with phone: XXXXX in the post data.

Change your server code to:

post '/registration/sms' do
  code = Random.rand(1111..9999).to_s
  phone = params[:phone].to_s
  HTTParty.get('http://sms.ru/sms/send?api_id=' + api_id +'&to=' + phone + '&text=' + code)
  # check code
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top