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': '[email protected]',
            '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

5
  • Make responseJson a global variable, so you can easily access variable in other function Commented Nov 17, 2014 at 9:29
  • possible duplicate of How to return the response from an Ajax call? Commented Nov 17, 2014 at 10:24
  • No it's not duplicating that question. What happen to the previous comments? Commented Nov 17, 2014 at 10:54
  • what you what to return ? Commented Nov 17, 2014 at 12:50
  • I want to take out the responseJson out from setKsonSer() to an outside JavaScript Function name Load(). Commented Nov 18, 2014 at 3:58

1 Answer 1

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': '[email protected]',
        '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); 
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.