Ajax call and Browser redirect issue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rakesh2k78
    New Member
    • Jan 2012
    • 4

    #1

    Ajax call and Browser redirect issue

    Hi All,

    I have a requirement where I need to fire an ajax call(dojo xhrPost)on click of an anchor, but as soon as I click the anchor link browser starts to redirect but ajax call is not finished yet, so I always got bad http return code 0 from the server. I can see in the firebug/developer tool.
    Is there any way to stop browser redirect until the ajax call finished or returned a proper response like 200.

    here is the code in dojo

    Code:
    function submitForm{
     dojo.xhrPost({
     url: some url,
     form : someform,
    
     load(function(response, ioArgs){
      //some stuff
     }),
     error(function(response, ioArgs){
      //some stuff
     })
    
    
     });
    }
    I am calling this on click of the anchor tag but browser navigates before the ajax return some proper response.

    pls suggest any solution for this.

    thanks,
    Rakesh
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the simple solution is: don’t trigger it with the href attribute. if there is no href, the link can’t load a new page.

    Comment

    • rakesh2k78
      New Member
      • Jan 2012
      • 4

      #3
      Thanks Dormilich, but I need to navigate as well, basically I want that AJAX call finished first and then only browser navigate to some x page.Can you pls help with this scenario?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        make it a synchronous call. but if you leave the page anyways, what good does the AJAX call do?

        Comment

        • rakesh2k78
          New Member
          • Jan 2012
          • 4

          #5
          Hi Dormilich, thanks for the reply.

          Actually, I am submitting a form, but there is situation where user either click on submit button or navigate from navigation bar(anchor tags in terms of menus) and there is no validation on form but we still need to submit default selections. We are using Java Page flow(Apache beehive) controller for form submission and one action is called for normal form submission on click of submit button but on click of navigation there is a different Controller's action is invoked which is taking the user to some screen, that's why I am submitting the form in an asynchronous way. Even though call is made asynchronously and form's values are saved in DB but the response from the server(bad http code 0) and not 200 which is problematic for us and even this call is not happen on all browser, it fails on Safari(Windows XP). Can you pls help with this?

          Comment

          • rakesh2k78
            New Member
            • Jan 2012
            • 4

            #6
            Hi All, I solved this issue.

            here is the solution :

            I returned false on click of anchor and then in dojo.XhrPost i did something like this

            load(function(r esponse, ioArgs){
            window.location .href = navUrl;
            }),

            where navUrl is the url of the anchor where we need to navigate.

            Comment

            • sai163
              New Member
              • Mar 2013
              • 1

              #7
              Please Elaborate

              Hello,

              Can you elaborate a bit more on how you solved this?
              I am facing the same issue.
              Using dojo.xhrpost. Form values are getting saved, but i getting no response. xhr.status code is "0". It works fine in my localhost, but when i pushed the code on server i am getting no response, though data is getting saved and redirection is successful. Can you tell me why the status code is set to '0'.

              Originally posted by rakesh2k78
              Hi All, I solved this issue.

              here is the solution :

              I returned false on click of anchor and then in dojo.XhrPost i did something like this

              load(function(r esponse, ioArgs){
              window.location .href = navUrl;
              }),

              where navUrl is the url of the anchor where we need to navigate.

              Comment

              Working...