我需要帮助我正在执行的ajax调用,我从后端传回一个数组,如下所示,如果我提醒前端的数据我得到了这个。
然而,当我通过它时,谷歌图表告诉我它不是一个数组。
Uncaught Error: Not an array
at gvjs_oba (format+en_GB,default+en_GB,ui+en_GB,corechart+en_GB.I.js:272)
at new gvjs_Pl (format+en_GB,default+en_GB,ui+en_GB,corechart+en_GB.I.js:274)
at drawChart (Default.aspx:60)
at Object.success (Default.aspx:43)
at j (jquery-1.11.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.11.1.min.js:2)
at x (jquery-1.11.1.min.js:4)
at b (jquery-1.11.1.min.js:4)
at Object.send (jquery-1.11.1.min.js:4)
at Function.ajax (jquery-1.11.1.min.js:4)
Ajax查询
<script lang="javascript">
var chart_data;
var startdate = "default";
var enddate = "default";
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(load_page_data);
function load_page_data()
{
$.ajax({
type: "post",
url: 'feed.aspx/GetJsonwithStringBuilder',
data: JSON.stringify({y: $('input[name=yaxis]:checked').val()}),
contentType: "application/json; charset=utf-8",
dataType: "text",
async: false,
success: function (data) {
if (data) {
drawChart(data, "My Chart", "Members");
}
},
error: function () {
alert('error');
}
});
}
function drawChart(chart_data, chart1_main_title, chart1_vaxis_title) {
var chart1_data = new google.visualization.arrayToDataTable(chart_data);
var chart1_options = {
title: chart1_main_title,
hAxis: { title: 'Month', type: 'string' },
seriesType: 'bars',
vAxis: { title: chart1_vaxis_title, titleTextStyle: { color: 'red' } }
};
var chart1_chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
chart1_chart.draw(chart1_data, chart1_options);
}
</script>
这是后端
[WebMethod()]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetJsonwithStringBuilder(string y)
{
DataTable dsChartData = new DataTable();
StringBuilder strScript = new StringBuilder();
try
{
dsChartData = GetChartData();
strScript.Append("[[");
foreach (DataColumn column in dsChartData.Columns)
{
strScript.Append("\"" + column.ColumnName + "\",");
}
strScript.Remove(strScript.Length - 1, 1);
strScript.Append("],");
foreach (DataRow row in dsChartData.Rows)
{
strScript.Append("[");
foreach (DataColumn column in dsChartData.Columns)
if (column.ColumnName == "Month")
strScript.Append("\"" + CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(Int32.Parse(row[column.ColumnName].ToString())) + "\",");
else if (IsNumber(row[column.ColumnName].ToString()))
strScript.Append("" + row[column.ColumnName] + ",");
else
strScript.Append("\"" + row[column.ColumnName] + "\",");
strScript.Remove(strScript.Length - 1, 1);
strScript.Append("],");
}
strScript.Remove(strScript.Length - 1, 1);
strScript.Append("]");
return strScript.ToString();
}
catch (Exception ex)
{
}
finally
{
dsChartData.Dispose();
strScript.Clear();
}
return "";
}
答案 0 :(得分:1)
您需要使用 data.d 而不是成功功能中的数据
$.ajax({
type: "post",
url: 'feed.aspx/GetJsonwithStringBuilder',
data: JSON.stringify({y: $('input[name=yaxis]:checked').val()}),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function (data) {
var dataObj=JSON.parse(data.d);
if (dataObj) {
drawChart(dataObj, "My Chart", "Members");
}
},
error: function () {
alert('error');
}
});
答案 1 :(得分:0)
success: function (data) {
drawChart(JSON.parse(data.d), "My Chart", "Members");
},
你能试试吗?