使用jquery.ajax发送时如何在Rails控制器中获取PUT参数 [英] How to get PUT params in rails controller when sent using jquery.ajax

查看:88
本文介绍了使用jquery.ajax发送时如何在Rails控制器中获取PUT参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击按钮后,我将拥有

(function() {
$("#btnStep2").click(function() {

    number = $('#btnStep2').attr('number');
     var dataString='number='+number+'&reserved='+$('#rd'+number).is(':checked')+'&environment=qa1';
    alert(dataString);
$.ajax({
    type: "PUT",
    url: "/tnrepos/"+number,
    contentType: "application/text; charset=utf-8",
    beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
    data: dataString, 
    success: function(data){ 
        alert("update successfully");

    }});
    return false;
});
});

在服务器上看到

    Started PUT "/tnrepos/12345" for 127.0.0.1 at 2013-03-15 11:38:03 -0700
Processing by TnreposController#update as */*
  Parameters: {"id"=>"12345"}

但是我看不到传递其他参数..

but i dont see other parameters passed ..

在控制器中,我打印参数

in the controller i print params

"action"=>"update", "controller"=>"tnrepos", "id"=>"12345"}

为什么不存在其他参数?我在Firefox和Chrome上进行了测试.

Why are not other parameters present ? I tested on firefox and chrome.

推荐答案

$.ajax({
    type: "PUT",
    url: "/tnrepos/" + number,
    contentType: "application/text; charset=utf-8",
    beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
    data: {data : dataString} ,
    success: function(data){ 
        alert("update successfully");

    }});
    return false;
});
});

您会在上方看到数据的使用:"{data:dataString}".

You see the use of data: "{data : dataString}", above.

现在,您的params [:data]将具有dataString.

Now, your params[:data] will have the dataString.

我建议将它们传递为"{number:number,保留: $('#rd'+ number).is(':checked')}并在控制器上访问它们 其名称如params [:number],params [:reserved]等.

I would recommend passing them as "{number : number , reserved : $('#rd'+number).is(':checked') } and accessing them on the controller with their names like, params[:number], params[:reserved] and so on.

尝试一下,让我知道!

这篇关于使用jquery.ajax发送时如何在Rails控制器中获取PUT参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆