简体   繁体   中英

param is missing or the value is empty for:

I have a POST request from an api

I do not want to save anything in my application, i would just like to send a request to the data and get an ok

my script

<script type="text/javascript">
$('input[type=submit]').click(function(e){
e.preventDefault();
var value = $('#form').serialize()

console.log(value);
$.ajax({
    url : 'https://1broker.com/api/v1/order/create.php?',
    type: 'post',
    dataType : 'html',
    data: value,
    beforeSend : function(){
        $('.load').show('100');
    },
    success : function(pre){
        $('.load').hide('100');
    }
   });
  });
 </script>

here is my controller

private

def set_position
  @position = Position.find(params[:id])
end


def position_params
  params.require(:position).permit(:symbol, :margin, :direction, :leverage, :order_type, :referral_id).permit(nil)
end

end

and the form

<form class="form-horizontal" id="form" method="post">
      <select class="form-control" name="symbol">
        <option> </option>
        <option>USDJPY</option>
        <option>EURUSD</option>
      </select>
        <input type="text" class="form-control" name="margin">
      <select class="form-control" name="direction">
        <option> </option>
        <option>long</option>
        <option>short</option>
      </select>
      <input class="form-control" name="leverage">
      <input type="hidden" name="order_type" value="market">
      <input type="hidden" name="referral_id" value="9918">
      <input class="form-control" name="stop_loss">
      <input class="form-control" name="take_profit">
    <button type="submit" class="btn btn-primary">Send</button>
  </form>

in applicationController i'm using

protect_from_forgery with: :null_session

在此处输入图片说明

Try changing data: value to data: {position: value} in your ajax call.

$.ajax({
    url : 'https://1broker.com/api/v1/order/create.php?',
    type: 'post',
    dataType : 'html',
    data: {position: value},
    beforeSend : function(){
        $('.load').show('100');
    },
    success : function(pre){
        $('.load').hide('100');
    }
   });
  });

I believe the problem is that your ajax call doesn't match what your strong params are searching for.

  $.ajax({
    url : 'https://1broker.com/api/v1/order/create.php?',
    type: 'post',
    dataType : 'html',
    data: value,
    beforeSend : function(){
      $('.load').show('100');
    },
    success : function(pre){
     $('.load').hide('100');
    }
  });


  def position_params
    params.require(:position).permit(:symbol, :margin, :direction,:leverage, :order_type, :referral_id).permit(nil)
  end

You could either change your ajax call like Gokul M said

  data: { position: value }

or you could modify your strong params to not require position if you don't need to for other actions:

  def position_params
    params.permit(:symbol, :margin, :direction,:leverage, :order_type, :referral_id).permit(nil)
  end

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