Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In rails how can I send an AJAX request without a ? in the data?

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

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:

"NetworkError: 404 Not Found - http://10.0.0.24:3000/routes/?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.

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!

like image 371
Datsik Avatar asked Mar 12 '13 05:03

Datsik


People also ask

How can I call AJAX without data?

You remove the data, by removing the data:{}, $. ajax({ url: 'ListCustomer', error: function(xhr, statusText, err) { alert("error"+xhr. status); }, success: function(data) { alert(data); }, type: 'GET' });

Can we use AJAX without URL?

You can either specify the URL or not when sending the AJAX request.


1 Answers

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

function fetchMarker(id) {
  var data;
  $.ajax({
    type: "GET",
    url: '/routes/' + id,
    dataType: "JSON",
    success: function(data) {
      console.log(data)
    }
  });
}
like image 132
Mischa Avatar answered Oct 18 '22 12:10

Mischa