0

I have this below AJAX function that will access an ASMX web service and generate a JSON response. The JSON is generating well. I want to pass this generated JSOn to an outside variable that in another function.

function setJsonSer() {
                         var strWsUrl = 'https://www.googleapis.com/analytics/v3/data/ga?   
ids=ga%3A76546294&dimensions='+ 'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-
date='+retStartDate()+'&end-date='+retEndDate()+'&max-results=10';
    formData = {
            'Email': 'clientlink@client.com',
            'Password': 'password',
            'URL': strWsUrl
        };
             $.ajax({
            url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
            type: 'POST',
            data: formData,

            complete: function(data) {
                      var responseText = data.responseText;
        /****01****/  var responseJson = JSON.parse(responseText.match(/[{].*.[}]/));
          console.log(responseJson);

                      Load(JSON.stringify(responseJson));
            }
        });
    }

You can see that /****01****/ is showing the JSON response is passing to another variable within the same function. I tried 'return' statement but it was not helpful. So what do I have to do to solve this problem? Could you someone to solve this?

UPDATE

I want to pass this responseJson to the follwoing function Load(). It should replace the rowData value. So what are you guys suggesting me?

function Load(){

//----------------------------------------------- Rohan

 var labels = new Array();
        var values = new Array();
        var catogories = new Array();
        var arrayOfArray = new Array();


        var rowData = ??????????????;
    console.log("'RowData' is " + typeof rowData );  

        inData = JSON.parse(rowData);

        var count = 0;
        var headers = new Array();
        for (var i = 0; i < inData.columnHeaders.length; i++) {
            headers[i] = inData.columnHeaders[i].name;
        }

        var dates = new Array();
        var pageViews = new Array();
        var uniqueViews = new Array();
        for (var key in inData.rows) {

                dates[key] = inData.rows[key][0];

            pageViews[key] = parseInt(inData.rows[key][1]);
            uniqueViews[key] = parseInt(inData.rows[key][2]);

        }

        $('#container_2').highcharts({
            chart: {
                type: 'areaspline', zoomType: 'x'
            },
            title: {
                text: 'Pageviews and Bounces'
            },
            legend: {
                layout: 'vertical',
                align: 'left',
                verticalAlign: 'top',
                x: 150,
                y: 100,
                floating: true,
                borderWidth: 1,
                backgroundColor: (Highcharts.theme && Highcharts.theme.legendBackgroundColor) || 
  '#FFFFFF'
            },
            xAxis: {
                categories: dates,
                type: 'datetime',
                dateTimeLabelFormats: {
                    month: '%d %b',
                },
                tickInterval: 10,
                plotBands: [{ // visualize the weekend

                    color: 'rgba(68, 170, 213, .2)'
                }]
            },
            yAxis: {
                title: {
                    text: 'Visits'
                }
            },
            tooltip: {
                shared: true,
                valueSuffix: ' '
            },
            credits: {
                enabled: false
            },
            plotOptions: {
                areaspline: {
                    fillOpacity: 0.5
                }
            },
            series: [{
                name: 'Page Views',
                data: pageViews
            }, {
                name: 'Bounces',
                data: uniqueViews
            }]
        });
}

Thanks and regards, Chiranthaka

ChiranSJ
  • 195
  • 1
  • 3
  • 20

1 Answers1

1

Should use the CallBack Function

function functionWantToCallAjax()
{
      //Do some Operation
      setJsonSer(function(value){ //Do what u want to do with returned value });
}

function setJsonSer(callback) {
  var strWsUrl = 'https://www.googleapis.com/analytics/v3/data/ga?   
  ids=ga%3A76546294&dimensions='+ 'ga%3Asource&metrics=ga%3Ausers&sort=-ga%3Ausers&start-
  date='+retStartDate()+'&end-date='+retEndDate()+'&max-results=10';
  formData = {
        'Email': 'clientlink@client.com',
        'Password': 'password',
        'URL': strWsUrl
  };
  $.ajax({
   url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
   type: 'POST',
   data: formData,
   complete: function(data) {
                  var responseText = data.responseText;
    /****01****/  var responseJson = JSON.parse(responseText.match(/[{].*.[}]/));
                  console.log(responseJson);


                  Load(JSON.stringify(responseJson));

                  //Call Back function  
                  callback(responseJson); 
        }
    });
}
Krishjs
  • 358
  • 2
  • 17