I have some experience with RoR, although, I'm kind of a newbie in AJAX matters. My goal is to have a HTML5 + JavaScript client and a Ruby on Rails server running in different machines (eventually).
What I want is the JavaScript client to get contents from the server in JSON format, to be parsed afterwards. I already tried a bunch of things, like adding a "responseType", etc, but non of them worked.
My current JavaScript file is like this:
$(document).ready(function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://localhost:3000/contents", true);
xmlhttp.send();
alert(xmlhttp.responseText);
});
And my RoR app has a path "/contents" that calls the index function of contents_controller.rb, that is like this:
def index
@contents = Content.all.order('created_at DESC')
respond_to do |format|
format.html # index.html.erb
format.json { render json: @contents }
end
end
Is AJAX even the correct way to do this? I'm a bit lost.
Thanks a lot!