-3

I have a function which returns data to getDates(), after processing i want the data in a global variable but it's not working.

var bookedDates=[];
function getDates(result) {
     var bookedDates1=result.split(",");
     var bookedDates2=[];
    for(i=1;i<bookedDates1.length;i++){
        bookedDates2.push(bookedDates1[i]);
    }
    bookedDates=bookedDates2;
}
alert(bookedDates);

$.ajax({
type: "POST",
url: "getbookeddates.php",
cache: false,
success: function(result){
    getDates(result);
//dates booked 
}

bookDates is blank in this case but it should be an array with ["21","22"].

Rohit Mahto
  • 176
  • 1
  • 9
  • Your function isn't being called. Above the alert add a line `getDates(result);`, or modify your bookedDates definition `var bookedDates = getDates(result);`. – Ian Aug 20 '18 at 06:33
  • it is called from $.ajax() success callback – Rohit Mahto Aug 20 '18 at 06:35
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – 31piy Aug 20 '18 at 06:35
  • @RohitMahto -- Please edit your question and include the code there. Don't post code in comments. – 31piy Aug 20 '18 at 06:36
  • Still your alert happens before your ajax call. Put the alert inside the ajax success function and try again. – Ian Aug 20 '18 at 06:47
  • calling outside the getDates() and after ajax request still gives blank response – Rohit Mahto Aug 20 '18 at 06:57

1 Answers1

1

It looks like you have done a very silly mistake. You are declaring the function getDates which is taking a parameter result in it. But you are not calling the function So the inside of the function will not get executed until you call it.

Try to add a calling line of a function.

getDates(result);

before the alert line.

Digvijay Rathore
  • 637
  • 1
  • 6
  • 21