• Post Reply Bookmark Topic Watch Topic
  • New Topic

jquery $.post doubt  RSS feed

 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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
servlet.



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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66261
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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. In fact, I'd say it merely over-complicates things.

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.)
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

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.

Thanks again.


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..
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66261
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear,

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.


I think everything is clear now.

Thanks as always for your valuable input...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!