-1

I have the following ajax method. On success I want to set a global variable but it doesn't seem to work - console returns empty object. It only works if I define async to false. However I'd like to keep the ajax method asynchronous. How can I get this to work?

var appointment = {};

if ($("#Appointment").is(":checked")) {
     $.ajax({
            type: "POST",
            url: "someurl",
            contentType: "application/json; charset=utf-8",
            data: JSON.stringify({
                dateStart: moment()
            }),
           // async: false,
            dataType: "json",
            success: function(data) {
                ajaxCallBack(data);
            }
    });

    function ajaxCallBack(data) {
            var response = $.parseJSON(data.d);
            appointment = { startDate: response.startDate, endDate: response.endDate };
    }
}

console.log(appointment);
4
  • Possible duplicate of JavaScript: Global variables after Ajax requests Commented Oct 24, 2016 at 18:52
  • stackoverflow.com/questions/14220321/… Commented Oct 24, 2016 at 18:53
  • @Ted for your information it's not lazy at all. I've been scratching my head and googling for hours. If you can't be bothered to post any useful answers as some of the others have done then refrain from marking every question as potential duplicates. If any thing your the one who is lazy if you can't be bothered to submit a solution with some explanation. Commented Oct 24, 2016 at 20:04
  • @Ted Clearly you fail to spend adequate time reading peoples comments the same way as you are lazy to submit any useful answers. I repeat again - I've already come across the posts that you mention prior to submitting my own question. Maybe you fail to realize that it's not always glaringly clear to relate one answer to every question - hence why people use stackoverflow for some additional help and direction, not to be belittled by individuals like yourself. Commented Oct 25, 2016 at 8:27

3 Answers 3

1

Ajax happens asynchronously, meaning code that appears after it won't wait for it to complete. So your console.log is executing before the ajax has necessarily gotten what it needs to populate the object.

Try moving your console.log statement inside of the callback - put it right after the line where you set appointment.

Sign up to request clarification or add additional context in comments.

Comments

1

console.log() is triggered before ajaxCallback() and before appointment is set (ajax is asynchronous), to show appointment in console you can run:

function ajaxCallBack(data) {
        var response = $.parseJSON(data.d);
        appointment = { startDate: response.startDate, endDate: response.endDate };
        console.log(appointment);
}

or

function ajaxCallBack(data) {
        var response = $.parseJSON(data.d);
        appointment = { startDate: response.startDate, endDate: response.endDate };
        printAppointment();
}

// define as global:
function printAppointment() {
    console.log(appointment)
}

Comments

0

ajax is an asynchronous operation. So console.log will execute even before ajax success. console the variable from ajaxCallBack function

function ajaxCallBack(data) {
  var response = $.parseJSON(data.d);
  appointment = { startDate: response.startDate,
                  endDate: response.endDate };
  console.log(appointment);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.