开发者

How to prevent ajax driven form from being submitted twice?

I am taking my first steps with Ajax while working on a Grails application. I am using

<g:form ...>  
  <g:submitToRemote ... />
</g:form>

in the most simple way and it worked great out of the box (Thanks grails!). The problem is, that the Ajax call needs about 2 seconds to return and update the html (at least on the test system) and during that time, the user can (开发者_如何学运维and actually does quite often) hit the submit button again. At the moment this results in a 2nd call being made that finally ruins the output (an error msg says, that one cannot submit the same data twice).

What is the best way to prevent this?

Thanks in advance for your input!


The best way to handle this is to disable the submit button in your onSubmit() function. Honestly, I don't know why more sites don't do this. I often go the next step and instead of disabling the submit button, I put the submit button in a span tag and replace the contents of the span tag with static text "Please wait..." That way your users get visual feedback they pressed the button and "top men are working on it".


As dj_segfault said you can do that.

If you want to validate this in your controller, take a look in "Handling Duplicate Form Submissions" in the official docs:

http://grails.org/doc/latest/guide/theWebLayer.html#formtokens

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜