tags:

views:

44

answers:

2
+1  Q: 

Ajax Post Doubt

Hi,

I still am confused regarding Ajax technology. And I still cannot answer this question of mine as I am still trying to grasp the technology.

Its regarding posting data with the server. In normal non-ajax web app, I usually do PRG (Post-Redirect-Get) pattern when I am doing a POST command. I have learned that this will prevent double submit problem or when the user tries to refresh the page or perform some Back-Forward transaction at the browser.

In Ajax, you do not perform a redirect because this will defeat the purpose of ajax as this is what the tutorials and books that I am folowing is saying

In Jquery, when I perform

$(document).ready(function(){
    $("#myForm").submit(function(){
        $.post("test.htm", function(data){
            alert("Successfully save data");
        }, "json");
        //prevent page refresh
        return false;
    });
});

and the user performs, refresh(F5) or user performs a Back and Forward browser transaction. Am I not risk of having double submit problem? Will the browser reload my click event again.

Sorry if this might sound too dumb to others but I just want to clarify my thoughts for a newbie like me.

+2  A: 

It depends on where you put that code, if that code is on a click event handler for a button or something, then reloading the page will not re-trigger the click. If you just drop that code exactly as is on your page (or in a $(document).ready function) then, yes, reloading the page will re-submit that request.

Since ajax is triggered by a user action, double submitting isn't a problem unless the user performs that action twice. Since you're handling everything in javascript anyway, it is easy to remove that action - or the button the user is clicking - after they have done that action once.

Does that make sense? Hope that helps.

Matthew J Morrison
Hi Matthew, I saw this code from the net and have been following this for a while. I edited also my post and completed my code. Do you think this code is free from double submit problem or page refresh for that matter? Is this what you mean in your post?
Mark Estrada
The refresh/double submit issue is typically due to the fact that when a user submits a form and the page is re-rendered, the first step in rendering that page was submitting the form - in your case this will not happen because you're submitting the form "behind the scenes" via javascript then stopping the actual html form from submitting and reloading the page. Having said that, a "double click" would still be possible - if the user clicks your submit button twice, they're going to run your code twice.
Matthew J Morrison
Oh I forgot about that... So another possible scenario is that, when I click the submit button and it takes time for the server to process my post data, then user clicks the submit button again, a re-post will happen right? So what if I do this, on first click, I disable the submit button? Will this be a nice added guard for a double ajax post? I remember, I was doing this before I learn about the PRG thinking that this will prevent double submit on non-ajax app. Thanks and have patience on me please. Things are getting clearer now. =)
Mark Estrada
Correct, disabling the button should work.
Matthew J Morrison
A: 

Typically, that post would occur within javascript and not automatically within a page refresh (unless the code processes it every time of course) - for instance it would process as part of another event such as a "click" or entry of a field on a form etc.

Now, as to how to handle posts of duplicate data, that is up to you on your server side code logic construction if posting of duplicate data is invalid.

Keep in mind that ajax is NOT typically part of a page round trip, but specifically to avoid such - so that you can do partial saves, without a page refresh for instance.

Mark Schultheiss
Hi Mark, your post keeps me thinking. In my server side logic, I used Spring MVC. Now when I perform a post, I have this workflow: Get Posted Form Data -> Update DB -> Return a JSON response -> Browser GUI Updating based on the response. Is there any specific thing that I need to check? Thanks thanks.
Mark Estrada