我有以下AJAX函数,它将访问ASMX Web服务并生成JSON响应。 JSON正在顺利生成。我想将这个生成的JSOn传递给另一个函数中的外部变量。
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));
}
});
}
你可以看到/ **** 01 **** /显示JSON响应正在传递到同一个函数中的另一个变量。我尝试了'return'语句,但没有用。那么我该怎么做才能解决这个问题呢?你能有人解决这个问题吗?
更新
我想将这个responseJson传递给下面的函数Load()。它应该替换rowData值。那你们有什么建议我的呢?
function Load(){
// --------------------------------------------- - 罗汉
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
}]
});
}
谢谢和问候, Chiranthaka
答案 0 :(得分:1)
应该使用CallBack功能
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);
}
});
}