简体   繁体   English

如何使用Opal-JQuery AJAX发布/获取值

[英]How to use Opal-JQuery AJAX to Post / Get values

I'm attempting to post values to a server using opal-jquery and I'm not having much success. 我正在尝试使用opal-jquery将值发布到服务器,但没有太大的成功。 If I attempt a post I don't get anything back. 如果我尝试发帖,我什么也收不到。 A Get gives me the values in a less than useful string. Get给了我一个不太有用的字符串值。

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

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 ("=>"). 在GET中,您传递的是在URL字符串中插入的Hash实例,这意味着该实例将使用.to_s转换为字符串,因此服务器端sinatra尝试在找到等号的地方拆分参数,该参数在哈希火箭中找到(“ =>”)。

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. 对于POST,我建议检查浏览器调试工具以查看请求是否正确发送,然后还检查.params是否是从请求中获取POST数据的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM