-4

I have a situation where I want to have a global variable like a whose value I'll be fetching from jQuery Ajax from some server and then want the value to be available on call from other function. To visualize:

var a = [];

$(document).ready(function () {
  $.ajax({
    type: 'GET',
    url: 'some url',
    success: function (response) {
      a = response.somearray;
    },
    error: function () {
      alert("Not successfull");
    }
  });
});

function something() { // this function will be called onclick of a button in html
  console.log(a); // here I want the value that came from the server. But currently, it's printing empty array
}

What I have tried are as follow:

  1. Removing var keyword from (a) to make it global.
  2. Assigning no value (here empty array) to (a) declaration above (that turned out to be a blunderous mistake though).
  3. Removing (a) declaration completely from the top.

The thing to take care about is that something() function is being called from an HTML button on complete page load and after ajax call. So value is already available.

MiKr13
  • 1,297
  • 13
  • 20
  • Looks right, for the most part, what problem are you running into? – CertainPerformance Dec 19 '18 at 06:23
  • The problem I'm getting is that it's printing empty array. – MiKr13 Dec 19 '18 at 06:27
  • As long as the user clicks the button after the response comes back (which is likely unless the server is laggy), it *shouldn't* be empty. Is this the *actual code* you're using? – CertainPerformance Dec 19 '18 at 06:28
  • You have a bracket mismatch by the way, did you check the js errors? – antoni Dec 19 '18 at 06:30
  • This is a demo code @antoni so bracket mismatch is not something I am interested in. The response from the server is coming with populated somearray already so I am interested in the functionality rather than syntax. – MiKr13 Dec 19 '18 at 06:54
  • yout code is working https://codepen.io/anon/pen/ZVByrb I can't find any problem with it. so the only reason for your code to not work is if somearray is empty array or the click event happens before the server returned a response – Nirit Levi Dec 19 '18 at 07:58
  • Possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Makyen Dec 19 '18 at 11:36
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321) – Makyen Dec 19 '18 at 11:37

2 Answers2

1

Here is a working example of what you want, check the console logs: https://codepen.io/antoniandre/pen/NebpQj?editors=0010

var a = [];

$(document).ready(function () {
  $.getJSON("https://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?", {
    tags: "snowboarding",
    tagmode: "any",
    format: "json"
  })
  .done(function (response) {
    a = response.items;
    // something(); Don't need this.
  });
});

function something() { // this function will be called onclick of a button in html
  console.log(a); // here I want the value that came from the server. But currently, it's printing empty array
}

EDIT:

HTML:

<button onclick="something()">hello</button>
antoni
  • 5,001
  • 1
  • 35
  • 44
-1

Scope was lost, since ajax callback is asynchronous function. I think, it should be

var a = [];

$(document).ready(function () {
  var me = this;
  $.ajax({
    type: 'GET',
    url: 'some url',
    success: function (response) {
      me.a = response.somearray;
    },
    error: function () {
      alert("Not successfull");
    });
});