Link to home
Start Free TrialLog in
Avatar of skillilea
skillilea

asked on

Global variable holding jSon return

I am trying to make 1 call to the DB.  It returns data that I will use throughout the page.

I want to put the results in a global var...is this possible.

here is what I have:

var fbData;

//----------------------------------------------------/
//doc ready
//----------------------------------------------------/
$(function () {
    //get all feedbackdata
    ReturnAllUserFeedback();

    //set summary sentence
    ResetSummarySentence();

});              //end doc

//----------------------------------------------------/
//return all feedback for the current user
//----------------------------------------------------/
function ReturnAllUserFeedback() {
    params = "{'uGUID':'" + outParams[2] + "','periodID':'" + outParams[1] + "'}";
    var p = "/Apps/Service/Core.asmx/ReturnAllFeedBackByUserGUID";
    $.ajax({
        type: 'POST',
        url: p,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8',
        data: params,
        error: function (xhr, ajaxOptions, thrownError) {
            alert("Error in getting all feedback");
        },
        success: function (response) {
            if (response) {
                fbData = response.d;
**** this does return data...I can loop here if I want to
            }
        }
    });
}

//----------------------------------------------------/
//set the summary sentence
//----------------------------------------------------/
function ResetSummarySentence() {
    $coach.children().eq(1).html("asdfasdf asdf asdf  dasf asdf");

*******this is where I want to loop through the fbData var
    alert(fbData);
    /*
    var items = [];
    $.each(fbData, function (indx, val) {
    ...switch on val.dType
        items.push(t);
    });
    */
}

Any thoughts or help...thanks tons!
Avatar of skullnobrains
skullnobrains

to make a variable global in js, just declare it with the "var" keyword outside of a function scope, and use it without redaclaring it from inside the function.
Avatar of skillilea

ASKER

it is at the top...

var fbData;

//----------------------------------------------------/
//doc ready
//----------------------------------------------------/
$(function () {

...see above
ASKER CERTIFIED SOLUTION
Avatar of skullnobrains
skullnobrains

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial