Here is how I’m making an ajax request
#action
def get_item
if request.get?
binding.pry #it always stop here, so it's working
item = Item.where(...)
unless item
item = Item.new
# .....
end
respond_to do |format|
format.json { render(json: item) }
end
elsif request.post?
# ......
end
end
#view
$.ajax({
type: "GET",
url: "/contr/get_item",
data: {key1: "value1"},
//datatype: "json",
success: function(data){
console.log("ajax success, data -> " + data[0]);
}
});
Although the code within get_item is executed, the data value at the page is always undefined.
What did I miss?
p.s. Note that the request is being sent back from server in json. I can see it in Chrome by “debug tool” by clicking F12 and going to Network tab.
Try this:
view