0
  var url_string = '/index.php/dvs/get_dvs/' + id + '/';
        $.ll = {};
        $.ll.dvs_data  = {};
        $.post(url_string,{},
        function (data)
        {
                $.each(data.dvs, function(k,v) {
                $.each(v, function(dvs_name, dvs_value) {

                    $.ll.dvs_data[dvs_name] = dvs_value;

                });
            });

        }, "json");

        var test = $.ll.dvs_data;
        console.log(test['username']);
        console.log(test.username);
        console.log(test);

The above is part of some other code, the data is being received as console.log(test) displays the object in firebug (firefox) however I am unable to access the test.username or test['username] objects, they just get returned as undefined

2
  • can you check firebug and give us the full return of $.ll.dvs_data Commented Aug 9, 2010 at 9:49
  • you seriously create your own namespace, augmenting the jQuery contructor like is bad karma. Commented Aug 9, 2010 at 9:53

1 Answer 1

2

This is a common mistake when it comes to AJAX. The post call is asynchronous, meaning it will continue to run the code after it, even before a response from the server is received.

Any code that depends on a response from the server needs to go inside the callback function:

$.post(url_string,{},
    function (data)
    {
        $.each(data.dvs, function(k,v) {
            $.each(v, function(dvs_name, dvs_value) {

                $.ll.dvs_data[dvs_name] = dvs_value;

            });
        });

        // Move your code up here
        var test = $.ll.dvs_data;
        console.log(test['username']);
        console.log(test.username);
        console.log(test);

    }, "json");
Sign up to request clarification or add additional context in comments.

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.