Q:

AJAX returns data only after first click

I have this ajax request script:
    function SendData(){
$( "#Submit" ).click(function() {
     $('#Submit').attr("disabled", true);
    $.ajax({
        type:"POST",
        url:"assets/process.php",
        data: {
            Years : $('#Years').val()
        },
        success: function(Response){
            if (Response.Status == 'Error') {                
                swal("Ups!", "Nemo' zebavaš", "error");
            } else if (Response.Status == 'Error0') {
                  swal("Ups!", "Servis 'Misterije' mogu koristiti samo rođene osobe!", "error");
            }
            else if (Response.Status == 'Error120') {
                  swal("Ups!", "Žao nam je! Niste podobni za korišćenje WEB Servisa 'Misterije'!", "error");
            } 
            else if (Response.Status == 'Success') {
                   swal("USPEŠNO!", 'Rođeni ste: '+Response.Calculated+' godine!', "success");
            }
            $('#Submit').attr("disabled", false);
        }
    });
});
}

When I load page in browser, add data to input and click on button nothing happens, but on second and all clicks after until I refresh page all working OK.
How I can slove this problem?

A:

Don't call the function
Put your code in $(document).ready function

