Home Rails remote renders a page of json and the JS ajax handler doesn't catch
Reply: 0

Rails remote renders a page of json and the JS ajax handler doesn't catch

user1078
1#
user1078 Published in June 19, 2018, 4:16 pm

I would like a form to post remotely, and then get some JSON back that my JS knows what to do with. Code:

<%= 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

When I do the POST, what happens is I correctly get the JSON, but in a page of just JSON (URL: http://localhost:3000/admin/order_lookup).

Obviously I don't want that, I want the ajax event handler to catch the JSON. What am I doing wrong?

I've tried

respond_to do |format|
  format.js {render json: @result}
end

in like every possible way, and I keep getting ActionController:UnknownFormat. Haven't successfully debugged that according to conventional logic either.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.334101 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO