在rails中,如何在没有a的情况下发送AJAX请求?的数据?

时间:2022-06-25 06:13:39

I'm trying to send a GET request to my /routes/ controller so I can receive some data back, right now I have

我正在尝试向我的/route / controller发送一个GET请求,这样我就可以接收到一些数据,现在我已经有了

function fetchMarker(id) {
    var data;
    $.ajax({
        type: "GET",
        url: '/routes/',
        data: id,
        dataType: "JSON",
        success: function(data) {
            console.log(data)
        }
    });
}

But the problem is is when I do that, Firebug tells me:

但问题是当我这么做的时候,Firebug告诉我:

"NetworkError: 404 Not Found - http://10.0.0.24:3000/routes/?15"

"NetworkError: 404未找到- http://10.0.0.24:3000/路由/?15"

I believe this is being caused by the ?, I've recently switched to Ruby on Rails so I don't know if this is normal but rake routes tells me it has to be /routes/(params[:id]) so I'm assuming just the ID number.

我认为这是由?引起的,我最近切换到Ruby on Rails,所以我不知道这是不是正常的,但是rake路由告诉我它必须是/route /(params[:id]),所以我假设只是id号。

My controller:

我的控制器:

def show
    @route = Route.find(params[:id])
    respond_to do |format|
        format.html
        format.json { render json: @route }
    end

end

Thanks in anticipation!

感谢期待!

1 个解决方案

#1


12  

Just append the id to the url, instead of sending it as data:

只需将id附加到url,而不是作为数据发送:

function fetchMarker(id) {
  var data;
  $.ajax({
    type: "GET",
    url: '/routes/' + id,
    dataType: "JSON",
    success: function(data) {
      console.log(data)
    }
  });
}

#1


12  

Just append the id to the url, instead of sending it as data:

只需将id附加到url,而不是作为数据发送:

function fetchMarker(id) {
  var data;
  $.ajax({
    type: "GET",
    url: '/routes/' + id,
    dataType: "JSON",
    success: function(data) {
      console.log(data)
    }
  });
}