简体   繁体   中英

Not able to post data using ajax in ruby on rails

Hello I have included given code in my view

= 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}

and in my js file inlcuded

$('#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'  
        });     
    } 
})

and in my controller

def update_express_shipping
    byebug
    a = '123'
  end

Now when I click on radio button and it is checked so I am not receiving data in my controller it gives only this params {"controller"=>"spree/checkout", "action"=>"update_express_shipping"}

Please let me know where I am wrong. Thanks in advance.

for radio buttons you should use change() instead of click()

replace

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

with

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

You can try following syntax of $.post

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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