0

I am getting data from an AJAX source and then assigning it to a global variable which doesn't seem to be working. Can someone please point me into the right direction?

var products = [];
    var count = 0;
    $.ajax(
    {
        url: 'url',
        success: function(response)
        {
            prCallback(response);

        },
        dataType: 'jsonp'
    });
    function prCallback(response)
    {
        window.products = response;            
    }
    console.log(products);

products is still an empty array, despite assigning value.

Ionut Necula
  • 11,107
  • 4
  • 45
  • 69
Masinde Muliro
  • 1,175
  • 3
  • 24
  • 38
  • 2
    You execute your console log call before the AJAX call is done – j08691 Apr 28 '16 at 13:16
  • Move the `console.log` inside callback; – Tushar Apr 28 '16 at 13:16
  • Please, provide a better title – Walter Macambira Apr 28 '16 at 13:18
  • 2
    @Ozrix: That's a matter of opinion. Basic debugging would have identified the issue immediately. A breakpoint on the `console.log`, and a breakpoint when setting `window.products`, and see which one is hit first. (Additionally, the question title doesn't identify the problem *at all*.) – David Apr 28 '16 at 13:18
  • 1
    @David, we're not living in a beige world where everyone has the same skillset, so before going all nuclear on newbies, try to reason and explain yourself. This is very discouraging for new people trying to get their questions answered. – Ozrix Apr 28 '16 at 13:19
  • 2
    @Ozrix: Issuing a down vote is hardly "going all nuclear". Note that I also voted to close the question as a duplicate of a very canonical one with excellent answers. Pointing users in the direction of such answers is a great way to help users. Complaining about the community isn't. – David Apr 28 '16 at 13:24
  • @David Quite off-topic, but what's the stance on linking google.com as answer for some questions? Not trying to be funny by the way, what if I actually think the user should just Google it but it's not a duplicate for example? – Strah Behry Apr 28 '16 at 13:27
  • 3
    @StrahBehry: Just linking to a Google search is generally frowned upon. (Referred to as "let me Google that for you" answers.) As an answer, definitely bad (likely to be down-voted). As a comment, generally considered ill-mannered unless some description is provided. Such as: "A Google search for 'X Y Z' is very likely to find helpful information. There are many tutorials available, and we'll be happy to help when you get stuck somewhere specific." But *just* a link to Google? If I remember correctly, there may even be a filter to prevent that comment from being submitted. – David Apr 28 '16 at 13:30

2 Answers2

4

You are logging before the response has returned. Move the console.log inside the callback.

The ajax request is async, it means that is has not yet returned when you call console.log.

Walter Macambira
  • 2,574
  • 19
  • 28
-2

Assigning values to global variables is not a good idea.

However here in console.log you have variable products which you declared on the top. In callback you have assigned value to window variable products - not to variable declared on the top.

You can delete declaration - "var products = []" and you should see results in console.log. However it's a bad idea to store them in this way.

Griffi
  • 243
  • 2
  • 9
  • `window.variableName` is not different than just calling the variableName itself, if it is a global variable. It is simply used to make it obvious that it is a global variable. While global variables may be a bad idea in most cases, your answer is misleading for this question. – Tricky12 Apr 28 '16 at 13:23
  • Yes, but in above example we have declaration "var products = []" and now products is not a window variable, but a global for whole of this scope. – Griffi Apr 28 '16 at 13:25
  • The OP's issue is certainly caused by asynchronous behavior. This answer is only correct (but again, not sufficient, since it does not address the known asynchronous issue) if the OP's code is in a non-global scope. – apsillers Apr 28 '16 at 13:26
  • 1
    It is irrelevant whether the global variable is initialized with or without window. They are still the same global variable. `var global = {data: 0}; alert(global === window.global); // displays "true"` - https://developer.mozilla.org/en-US/docs/Web/API/Window/window – Tricky12 Apr 28 '16 at 13:28
  • Sorry, but you are not correct. That var declaration still adds the property to the root (window). – Walter Macambira Apr 28 '16 at 13:28
  • 2
    Global variables declared with `var` behave as properties of `window`, but in any case that whole issue is a red herring: the problem is the order in which the asynchronous code is executed. – nnnnnn Apr 28 '16 at 13:29