开发者

Ajax Post Doubt

开发者 https://www.devze.com 2023-01-11 11:48 出处:网络
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.

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 开发者_如何学Cdefeat 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.


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.


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.

0

精彩评论

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

关注公众号