Currently I am using jquery's $.get and $.getJSON when fetching data from the database and jquery is really good in doing this.
Now, I have some doubts regarding the $.post method that I would like to clarify.
Oftentimes, I used post method when I need to update/delete or change any state in the server,
afterwhich I always redirect to a sucess page. PRG pattern as they say.
I have these questions though, hope somebody could enlighten me on this as I cant find specific resource on the net that will answer my question
1. The point of ajax is to avoid page refreshes. Does this mean that I should edit my servlet controller and avoid the redirect? This is in my doPost method of my
2. Double submit problem. If I intentionally refresh my page, theres no harm in doing this right even if I use $.post? I know that the $.post method is usually binded to the event handler of any button click so this wont submit itself again and call the $.post.
If theres any link or tutorial where I could read on about this, I would really appreciate it. Thanks.
I rarely (in fact, probably never) have a POST request designed such that it is used for both Ajax and page-level requests. And I do not use PRG for those POSTs that are designed for Ajax.
(I will religiously use PRG for page-level POSTs though.)
The PRG pattern is primarily to avoid letting the POST sit at the top-level of a page, susceptible to the refresh issue. There's really no reason or advantage to using it for Ajax requests
This really got me thinking. But I just want to clear my thoughts if you may allow.
Since ajax post request are binded on the event handler of any button/link/submit event, they really are not resent back to the server even if user refresh their browser? Can I add this explanation into your explanation already?
I rarely (in fact, probably never) have a POST request designed such that it is used for both Ajax and page-level requests
Got this point.
P.S. I raised this question because my jquery in action book only covers the $.post command in 2 pages. I *personally* think that many request are post-ed.
My wish is that more explanation was added since post request has many intricacies. But of course, as an avid fan of yours this is just a wish..
mark reyes wrote:Since ajax post request are binded on the event handler of any button/link/submit event, they really are not resent back to the server even if user refresh their browser?
No. A refresh will just reload the browser from the source HTML. It's not going to replay every event that happened to the page after its was loaded.
P.S. I raised this question because my jquery in action book only covers the $.post command in 2 pages.
I hear you, but not only was the Ajax chapter already much longer than the publisher was happy with (you should have heard them when they found out I needed 3 chapters to cover jQuery UI in the 2nd edition), but the machinations of the server-side code (where the PRG pattern is implemented) is well beyond the scope of the book.
That why I write articles!