1

I've following code

submitHandler: function(form) {
                    $.ajax({
                        url:        GLOBAL_URL + '/searching',
                        data:       $(form).serialize(),
                        type:       'POST'
                    });

                    window.location.href = GLOBAL_URL + '/complete';
                }

What I'm trying to do is when the form gets submitted after validation, I want the user to be redirected to other page while it's searching in the background.

But it doesn't. It waits for the ajax to complete the request and then it redirects.

This ajax works fine out of the jquery validation plugin but now inside. I also tried to set async: true. But no help.

I don't want the user to wait while it's searching. I just want to redirect the user as soon as user submits the form and ajax is fired successfully.

Help me with this. Thanks

3
  • add ajaxComplete() Commented Mar 20, 2015 at 10:52
  • What's the point of using ajax() if you still want the page to redirect? Just use a standard form submit. Commented Mar 20, 2015 at 19:10
  • Please do understand. Standard submission will keep the user waiting until server gives a response which is a long while. That is why the reason I send an email as response to their submission. Commented Mar 20, 2015 at 19:33

4 Answers 4

4

You can use the success callback for the redirection.

$.ajax({
    url: GLOBAL_URL + '/searching',
    data: $(form).serialize(),
    type: 'POST'
}).done(function () {
    window.location.href = GLOBAL_URL + '/complete';
});
Sign up to request clarification or add additional context in comments.

9 Comments

I've tried that too sir. I don't want the user to wait while it's searching. I just want to redirect the user as soon as user submits the form and ajax call is fired. Thank you for your reply.
in that case you use a timer and redirect in that like setTimeout(function(){ window.location.href = GLOBAL_URL + '/complete'; }, 500)
Thanks for quick response. That way it waits for the ajax to complete and extra 500ms to redirect.
@m3huL that should be used only if the ajax processing is very long... in such case irrespective of the ajax completion
That is the case. Ajax processing takes a while as it calls few apis and does complex database operations. Any solution?
|
0

Try this,

$.ajax({
    url:        GLOBAL_URL + '/searching',
    data:       $(form).serialize(),
    type:       'POST',
    success:     function(response){ // called when you get the response from server
        // you can check the response and redirect your page accordingly
        // for success or error response
        window.location.href = GLOBAL_URL + '/complete';
    }
});                    

1 Comment

I don't want user to wait till server sends back the response. It's like fire and forget. Any idea?
0
$("#submit_fund").click(function(){
    $('#frm_add_fund').ajaxForm({
        dataType:  'json',
        success:   test
    });
});

function test(data) {
  if(data.success == 0 ){

  //show the validation error for particular fields//

  }else if( data.success == 1){
         var url = data.url;
    window.location = url.replace('&&', '&').replace('?&', '?');
  }
 }

2 Comments

Could you explain a bit?
I have use ajax form submit, in the success function , if there is no validation error, then it will redirected.
-1

Remove whole submitHandler block and add click handler separately for button

$("#yousubmitbtnid").click (function() {

    If ($("#yourformid").valid ()){

                    .ajax({
                        url:        GLOBAL_URL + '/searching',
                        data:      $("#yourformid") .serialize(),
                        type:       'POST'
                    });

                    window.location.href = GLOBAL_URL + '/complete';
    }

});

If problem occurs, change input type submit to button Sorry for bad indentation. As I am using mobile for this

4 Comments

This is the exact opposite of a correct answer. The OP is using the plugin's submitHandler exactly for what it was designed... ajax().
But OP says submitHandler is not allowing ajax async and he told " This ajax works fine out of the jquery validation plugin but now inside. I also tried to set async: true. But no help" means above code does same as submitHandler but outside..and async too
Bottom line, to avoid redundancy and conflicts use the plugin's submitHandler for ajax()... that's what it's for. The OP's is clearly confused about ajax() here, as if you simply want to redirect to a new page, you wouldn't use ajax() in the first place.
I think so but his desire is to perform redirect and ajax call in parallel way. This way he will not able to grab the response of ajax but can initiate ajax call in background and redirect same time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.