0

I am calling ajax in one function and the result should be caught in another function. It is better explained below

function myMain(){

    var test = myWPackage();
    alert(test);
 }


function myWPackage(){

var tender_number = "TENDER_TEST-123";

//calling ajax function
            $.ajax
                        ({
                            url: "getWorkPackage.php",
                            type: "POST",
                            dataType: 'json',
                            data: {source1 : tender_number},
                            cache: false,
                            success: function (work_package) 
                            {
                                return work_package[0];

                            })//ajax ending

}

The database if connected and the value is coming if i replace return with alert in myWPackage so there is no issue with database or the data coming.

When i call myMain() function it gives "UNDEFINED". Can anyone let me know what am i doing wrong? I am trying to ge the value from ajax to test.

thank you in advance.

Alz
  • 351
  • 5
  • 17
  • I have already seen the answer and it did not help me. I am not passing a parameter in the first function. I want to call the 2nd function in the first and get its value in a variable. – Alz Oct 20 '14 at 11:16

1 Answers1

0

I think the problem is of synchronization , Because Ajax in jquery if not synchronize Try this link

Community
  • 1
  • 1
  • Do i have to put a delay somewhere so the ajax code is executed and the result is taken before i put the value in the variable in the 1st function? – Alz Oct 20 '14 at 11:17