sk2212
sk2212

Reputation: 1722

php exec command does not work within ajax call

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?

Upvotes: 0

Views: 1139

Answers (1)

SLaks
SLaks

Reputation: 887415

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.

Upvotes: 2

Related Questions