开发者

Submitting form via AJAX and getting response

开发者 https://www.devze.com 2023-01-06 09:51 出处:网络
In my rails app, I need to submit a form via ajax so that the form submit happens behind the scenes and doesn\'t require a full page reload.

In my rails app, I need to submit a form via ajax so that the form submit happens behind the scenes and doesn't require a full page reload.

I did form_remote_tag, but I can't figure out how to get a response from it make sure it开发者_C百科 was successful. Is there a way to, or do people just trust it's always successful (eek!)?

Or is there a better solution?


When you're responding to the AJAX request - probably from a the create or update action of one of your controllers, you should respond with some javascript or JSON.

If you create an .rjs file to respond, it will get executed when the page responds. If everything went well, you can redirect or update the page. If something failed, replace the form with a partial which will display the errors. Read about rjs here: http://www.codyfauser.com/2005/11/20/rails-rjs-templates


Just bind a callback to your form_remote_tag that would process your response.

It works fine for me. Hope will help you out.

Here my code :

<% form_remote_tag :url => some_path, :method=>'post',:complete =>'your_callback_method();' do %>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号