【问题标题】:Not able to post data using ajax in ruby on rails无法在 ruby​​ on rails 中使用 ajax 发布数据
【发布时间】:2016-03-18 06:30:23
【问题描述】:

您好,我在视图中包含了给定的代码

= form_for @order, :url => update_express_shipping_path,:remote=> true, :html => { :id => "shippingDetails" } do |form|
    = form.radio_button :express_shipping, true, :value => true, :id => 'shipping_method4', :name => 'shipping_method'
    Express Shipping
    .checkOrder{:value=> @order.id}

并在我的 js 文件中包含

$('#shipping_method4').click(function(){ 
    if($(this).is(':checked')){
        alert ('123')
        $.ajax({
            url: '/update_express_shipping',
            type: 'POST',
            data: {
                'id': $('.checkOrder').attr('value'),
                'express_shipping': $(this).val()
              },
            contentType: 'json'  
        });     
    } 
})

在我的控制器中

def update_express_shipping
    byebug
    a = '123'
  end

现在,当我单击单选按钮并选中它时,我没有在控制器中接收数据,它只给出这个 参数 {"controller"=>"spree/checkout", "action"=>"update_express_shipping"}

请让我知道我错在哪里。提前致谢。

【问题讨论】:

  • 你能把日志放上去吗

标签: jquery ruby-on-rails ajax ruby-on-rails-4


【解决方案1】:

对于单选按钮,您应该使用 change() 而不是 click()

替换

$('#shipping_method4').click(function(){

与

$('#shipping_method4').change(function(){

【讨论】:

  • 您是只添加了一个单选按钮还是添加了多个。一个表单必须有多个单选按钮。如果您只添加一个。您必须要添加复选框而不是单选按钮。有了您的回复,我可以为您提供更好的帮助。
【解决方案2】:

您可以尝试以下$.post的语法

$.post('/update_express_shipping', { 'id': $('.checkOrder').attr('value'),
                'express_shipping': $(this).val()})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多