'How to call ASP.NET jQuery Ajax function from CodeFile Method ["website Project"]
I'm calling the ajax function using javascript in website project and the webmethod
aspx page is running on CodeFile="NewPLPage.aspx.cs"
and when I run the NewPLPage.aspx page I'm getting the following error
Error is :
Uncaught TypeError: Cannot read properties of undefined (reading '0')
Screenshot
website project example pic
Screenshot
I'm not sure where I did the mistake in the code
this my aspx code "NewPLPage.aspx" :
<figure class="highcharts-figure">
<div id="container"></div>
</figure>
javascript code :
$(document).ready(function () {
linedata();
}
function linedata() {
//var param = "Line";
var param = { Terms: $('#portperfgrphtab6M').text() };
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
url: "NewPLPage.aspx/GetLineChartData",
dataType: "json",
success: function (Result) {
Result = Result.d;
var data = [];
for (var i in Result) {
//var serie = new Array(Result[i].Names, Result[i].Values, Result[i].Dummy);
var serie = new Array(Result[i].Date, Result[i].Niftyperform, Result[i].Portfolioperform);
data.push(serie);
}
DreawLineChart(data);
$(".highcharts-credits")[0].innerHTML = "";
$(".highcharts-button-symbol").hide();
},
error: function (Result) {
alert("Something Went Wrong !");
}
});
}
/*Inter link of "linedata()"*/
function DreawLineChart(series) {
Highcharts.chart('container', {
chart: {
type: 'line'
},
title: {
text: 'Performance Graph'
},
//subtitle: {
// text: 'Source: WorldClimate.com'
//},
xAxis: {
//categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
// 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
categories: [series[0][0], series[1][0], series[2][0], series[3][0], series[4][0], series[5][0]] // Getting error : Uncaught TypeError: Cannot read properties of undefined (reading '0')
},
yAxis: {
title: {
text: ''
},
//labels: {
// formatter: function () {
// return this.value + '°';
// }
//}
},
tooltip: {
crosshairs: true,
shared: true
},
plotOptions: {
line: {
marker: {
radius: 4,
lineColor: '#666666',
lineWidth: 1,
}
}
},
series: [{
name: 'NiftyPerform',
marker: {
symbol: 'diamond'
},
//data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, {
// y: 26.5,
// marker: {
// symbol: 'url(https://www.highcharts.com/samples/graphics/sun.png)'
// }
//}, 23.3, 18.3, 13.9, 9.6]
data: [series[0][1], series[1][1], series[2][1], series[3][1], series[4][1], series[5][1]]
}, {
name: 'PortfolioPerform',
marker: {
symbol: 'diamond'
},
//data: [{
// y: 3.9,
// marker: {
// symbol: 'url(https://www.highcharts.com/samples/graphics/snow.png)'
// }
//}, 4.2, 5.7, 8.5, 11.9, 15.2, 17.0, 16.6, 14.2, 10.3, 6.6, 4.8]
data: [series[0][2], series[1][2], series[2][2], series[3][2], series[4][2], series[5][2]]
}]
});
}
this is NewPlPage.cs page code :
[WebMethod]
public static List<Performgraph> GetLineChartData(string Terms)
{
//string Terms
//string Terms = "";
DataTable dt = new DataTable(); //null;
//List<object> chartData = new List<object>();
//List<string> reports = new List<string>();
List<string> starttime = new List<string>();
List<Performgraph> performgraphs = new List<Performgraph>();
GetPerformgraphapp getPerformgraphapp = GraphData(); // Getting service data without any error
if (Terms == "6M")
dt = getPerformgraphapp.M6.ConvertToDataSet("Graph6M").Tables[0];
else if (Terms == "1Y")
dt = getPerformgraphapp.YR1.ConvertToDataSet("Graph1Y").Tables[0];
else if (Terms == "2Y")
dt = getPerformgraphapp.YR2.ConvertToDataSet("Graph2Y").Tables[0];
else if (Terms == "3Y")
dt = getPerformgraphapp.YR3.ConvertToDataSet("Graph3Y").Tables[0];
else if (Terms == "5Y")
dt = getPerformgraphapp.YR5.ConvertToDataSet("Graph5Y").Tables[0];
if (dt.Rows.Count > 0 && dt != null)
{
////Insert Label for columns in First position.
//reports.Insert(0, "LineChart");
//reports.Add("NiftyPerform");
//reports.Add("PortfolioPerform");
////Add the columns Array to the Chart Array.
//chartData.Add(reports.ToArray());
starttime = (from p in dt.AsEnumerable()
select p.Field<string>("DATE")).ToList();
string yyMMM = "";
foreach (string datetime in starttime)
{
List<object> niftyperform = (from p in dt.AsEnumerable()
where p.Field<string>("DATE") == datetime
select p.Field<double>("niftyperform")).Cast<object>().ToList();
//Should not use or convert the string data
//List<object> totals = (from p in dt.AsEnumerable()
// where p.Field<string>("DATE") == datetime
// select Convert.ToString(p.Field<double>("portfolioperform"))).Cast<object>().ToList();
List<object> portfolioperform = (from p in dt.AsEnumerable()
where p.Field<string>("DATE") == datetime
select p.Field<double>("portfolioperform")).Cast<object>().ToList();
yyMMM = Convert.ToDateTime(datetime).ToString("yyMMM");
//string[] yymmm = Convert.ToDateTime(datetime).GetDateTimeFormats();
//totals.Insert(0, Convert.ToDouble(niftyperform[0]));
//totals.Insert(0, yyMMM.ToUpper().ToString());
//chartData.Add(portfolioperform.ToArray());
performgraphs.Add(new Performgraph
{
Date = yyMMM.ToUpper().ToString(),
Niftyperform = Convert.ToDouble(niftyperform[0]),
Portfolioperform = Convert.ToDouble(portfolioperform[0])
});
}
//return chartData;
}
else
{
//Page page = (Page)HttpContext.Current.Handler;
//Control myDiv = (Control)page.FindControl("pfperformchart");
}
return performgraphs;
}
Class :
public class Performgraph
{
public string Date { get; set; }
public double Niftyperform { get; set; }
public double Portfolioperform { get; set; }
}
Give the best suggestion to resolve this error
I'm new to ajax
call and webmethod
concepts.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|