My goal is to have a form that can post remotely and then receive JSON data back for my JavaScript to process. Below is the code I am using:
<%= form_tag '/admin/order_lookup', remote: true, authenticity_token: true, id: "order-lookup-submit" do -%>
<%= text_field_tag 'contact_string' %>
<%= submit_tag 'Submit' %>
<% end -%>
$("#order-lookup-submit").on("ajax:success", function(e, data, status, xhr) {
alert("in success")
})
def order_lookup
# some code
render json: @result
end
After performing the POST request, I receive the expected JSON response, but it displays on a separate JSON page (URL:
http://localhost:3000/admin/order_lookup
).
My intention is for the ajax event handler to capture this JSON data instead of displaying it separately. Can someone point out what might be wrong with my approach?
I have attempted variations like
respond_to do |format|
format.js {render json: @result}
end
in various ways, but I keep encountering an ActionController:UnknownFormat error. I have not been able to debug this issue successfully following traditional methods either.