-1

I have the following code which works on the event that a select box is changed. When it changes, the ajax call will pass the results to the variable result. My problem is that I cannot get that data outside this function.

The code:

$('#myselect').on('change', function() {
    $.ajax({
        url: 'myurl'+this.value,
        method: 'GET',
        success: function(result) {
            console.log(result); //Just checking it's there
            external = result;
        }
    });
});

console.log(external); //Returning Undefined

I need external to be available here outside the function above.

How do I do this?

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Roberts Jul 26 '16 at 13:45

5 Answers5

1

You can create the variable outside of the event and than access it outside. But as ajax is ascychron, the variable would still be undefined:

var external;

$('#myselect').on('change', function() {
    $.ajax({
        url: 'myurl' + this.value,
        method: 'GET',
        success: function(result) {
            external = result;
        }
    });
});

// this would be still undefined
console.log(external);

You can write the console.log inside your success, or create your own callback function. Then you can log or handle the data otherwise after the ajax request.

$('#myselect').on('change', function() {
    $.ajax({
        url: 'myurl' + this.value,
        method: 'GET',
        success: function(result) {
            myCallback(result);
        }
    });
});

function myCallback(external) {
    console.log(external);
}

Even shorter would it be possible in this example, if you directly use your callback function as success callback without wrapper. And because GET is the default request type, we could remove it too:

$('#myselect').on('change', function() {
    $.ajax({
        url: 'myurl' + this.value,
        success: myCallback
    });
});

function myCallback(external) {
    console.log(external);
}
eisbehr
  • 12,243
  • 7
  • 38
  • 63
0

I'm sorry if I'm misunderstanding the question:

What it sounds like is that you are trying to get the data from external but the ajax call hasn't called the callback yet, so the data hasn't been set.

If I can recommend how to fix it, call the code that references external from the callback itself so you can guarantee that you have the data when you need it.

Patrick Barr
  • 1,123
  • 7
  • 17
0

You are not able to access variable because it comes in picture after ajax completion and you are trying to access it directly, since you don't have any idea how exact time this ajax takes each time so you need to define a function like this

$('#myselect').on('change', function() {
  $.ajax({
      url: 'myurl'+this.value,
      method: 'GET',
       success: function(result) {
       console.log(result); //Just checking it's there
       saySomething(result); //result will be injected in this function after ajax completion 
    });
});

function saySomething(msg) {
    console.log(msg); //use variable like this
}
eisbehr
  • 12,243
  • 7
  • 38
  • 63
Arpit Kumar
  • 2,179
  • 5
  • 28
  • 53
0

I hope my code is help to you.

$('#myselect').on('change', function() {
    $.ajax({
        url: 'myurl'+this.value,
        method: 'GET',
        async: false, // stop execution until response not come.
        success: function(result) {
            console.log(result); //Just checking it's there
            external = result;
        }
    });
});

console.log(external); //now it should return value
Haresh Vidja
  • 8,340
  • 3
  • 25
  • 42
-1

Define the variable external above your first function like so:

var external;

$('#myselect').on('change', function() {


  $.ajax({
        url: 'myurl'+this.value,
        method: 'GET',
        success: function(result) {

         console.log(result); //Just checking it's there

         external = result;


        }

    });

});


console.log(external);
seemc0
  • 27
  • 2