1

I have made several attempts to capture a json results into a global variable but it keep coming back undefined. Any clues to what I am doing wrong?

    var temp;
        var temp2;
    function getCount() {

       var jqxhr = $.getJSON("Data/OutageCountHandler.ashx", function({
temp = jqxhr.responseText; //attempt to assign data to global variable. returns undefined.
                 console.log("success");
        })
      .done(function (data) {
          console.log("second success");
          console.log(jqxhr.responseText); //returns the number but only in log   
          console.log(data); //returns the number but only in log 
          console.log(data.d); //returns undefined
          temp2= jqxhr.responseText; //second attempt returns undefined.
          temp3 = data; //returns undefined
          temp4 = data.d; //returns undefined
      })
       .fail(function () {
        console.log("error");
        })

      .always(function () {
          console.log("complete");
      });
            temp5 = jqxhr.responseText;  returns undefined.
        jqxhr.complete(function () {
            console.log("second complete");
        });
        temp6 = jqxhr.responseText; //returns undefined
        return jqxhr.responseText; //returns undefined
    }

I also tried this way but same results

function getCount() {
    var results = null;
    $.getJSON("Data/OutageCountHandler.ashx", function (data) {

        temp = data;     //returns null
        temp2 = data.d;  //returns undefined
        results = data; //returns null
    });

    return results; //returns null
}

1 Answers1

0
temp = jqxhr[0].responseText; //attempt to assign data to global variable. returns undefined.
                 console.log("success");

try adding in a [0] after jqxhr. that may fix your problem.

Ishiffman120
  • 313
  • 1
  • 14