Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery ajax post not executed

I have a little problem with jQuery ajax. I want to send a POST to a .php file and afterwards change the current URL. I tried the following code:

<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script> 
<script>
window.onload = function () {
    $.ajax({
        type: "POST",
        url: "sample.php",
        data: {'username': "STRING"},
        timeout: 1000,
        complete: function(){
            window.location.href = 'http://127.0.0.1/sample/another.php';
        }
    });
}
</script>

So, as I can see in the chrome developer tools Network tab, no POST is executed. Although the URL changed to another.php

Can you tell me what I am doing wrong or how I can solve this issue?

like image 905
Yanick Schraner Avatar asked Apr 22 '26 04:04

Yanick Schraner


1 Answers

use success instead of complete

 $.ajax({
        type: "POST",
        url: "sample.php",
        data: {'username': "STRING"},
        timeout: 1000,
        success : function(){
            window.location.href = 'http://127.0.0.1/sample/another.php';
        }
    });
like image 51
Anri Avatar answered Apr 23 '26 16:04

Anri