简体   繁体   English

如何通过ajax将数据发送到控制器ruby

[英]how to send data to controller ruby by ajax

In application.js : 在application.js中:

function get_cell_id(select_id)
{
  $.ajax({
      url: "/assign_variable",

      type: "GET",
       data:{channel:"abc"},
      error: function(){     
      alert('fail')
      }
  });
}

in static_pages_controller 在static_pages_controller中

def assign_variable
  @Four_Season_Set_Channel= params[:channel]
  end

in routes.rb 在routes.rb中

 match '/assign_variable', to:'static_pages#assign_variable', via: 'Get'

but i got alert fail, please help me fix it. 但是我收到警报失败,请帮助我修复它。

You might need to be rendering something, be it HTML/plain-text. 您可能需要呈现某些内容,例如HTML /纯文本。 That should solve the problem. 那应该解决问题。 Else you would be getting a 204 error (NO CONTENT). 否则,您将收到204错误(无内容)。 Also, in the javascript-error-function, try alert/log the error-code. 另外,在javascript-error-function中,尝试警报/记录错误代码。 That will help too. 那也有帮助。

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

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