简体   繁体   中英

JQuery posting form via AJAX not triggering rails respond_to format.js

I have a simple form with an order by param. When my dropdown is changed, I call this:

$.post("/busca", $("#order_form").serialize(), dataType: "script")

On the rails controller side I have a simple format.js to handle the ajax calls. Thing is that this isn't working. The js.erb template is never rendered.

My log shows Processing by BuscaController#index as */* and I have no idea what the */* stands for. Can someone help me?

Ok so I figured it out. When you have a respond_to block like this:

respond_to do |format|
  format.html
  format.js
end

It will not work. You need to setup the js response first than the HTML one. Don't ask me why. This is the one that works for me:

respond_to do |format|
  format.js
  format.html
end

Seems that all post parameters are mandatory:

jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )

I was able to get the js when doing this:

$.post("/busca", $("#order_form").serialize(), function() { }, "script")

No matter the format.x order

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM