I am trying to get an instance of a modal (in JSON format) by using Ajax when a form is submitted. Currently, on the alert, it outputs nothing, blank.
alert('<%= @modal_data.to_json.html_safe %>'); outputs JSON data, but alert(data); called through Ajax does not output anything.
Controller
class ModalController < ApplicationController
def modal_index
@modal_data = Modal.where(:var => params[:attr1])
respond_to do |format|
format.html
format.json {render json: @modal_data}
end
end
end
JavaScript
$(document).ready(function() {
$('#my_form').on("submit", function () {
$.ajax({
type: "GET",
dataType: "json",
url: "/modal/modal_index",
success: function(data){
alert(data);
}
});
});
});