0

I am using jQuery 2.0.2 with ajax to call a php page which executes a command to start a java client:

        $j(document).ready(function(){
            $j(".do_remote_sync").on("click",function(event) {                       
                var d = "";
                $j.ajax({
                    data: \'test=test2\',
                    type: \'POST\',
                    url: \'sync_remote.php\',
                    success: function(data) {
                        d = data;
                    }
                });                        
                //alert("after ajax");
            });
        });

The sync_remote.php page only contains following line:

exec("java -jar RemoteSync.jar config.properties", $output, $return_var);

The problem is now that the sync_remote.php page is only called if I write an "alert" command after the ajax call (alert("after ajax");).

If I comment out this line the ajax call is most probably not called!

The sync_remote.php page works without problems if a call it directly.

Question

Why the ajax call is not executed without the "alert" command?

1 Answer 1

2

This would happen if .do_remote_sync is a hyperlink.

Clicking the hyperlink navigates to its href, cancelling any active AJAX requests.
Adding an alert() gives the AJAX request enough time to make it to the server before that happens.

You need to return false to prevent the navigation.

Sign up to request clarification or add additional context in comments.

3 Comments

It is a submit form element: <input class="do_remote_sync" type="submit" />. Does it behave the same like a hyperlink?
@sk2212: Yes; it submits the form.
Thats the problem! Thanks!

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.