behzad razzaqi behzad razzaqi - 6 months ago 33
Ajax Question

How can I return list object to ajax call back function from asp.net web form?

I'm beginner in asp.net and write simple web application to show user any chart using highcharts tools, in highcharts need read data with ajax and for that purpose I write this code:


function FetchData() {
var pData = [];
pData[0] = $("#ddlyear").val();
var jsonData = JSON.stringify({ pData: pData });
$.ajax({
type: "POST",
url: "CS.aspx/GetDataBehzad",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess_,
error: OnErrorCall_
});
function OnSuccess_(response) {
var aData = response.d;
alert(aData);
}
function OnErrorCall_(response) {
alert("Whoops something went wrong!");
}
e.preventDefault();
}




and in
CS.aspx
write this code:


[System.Web.Services.WebMethod]
public List<int> GetDataBehzad()
{

List<int> list = new List<int>();
list.Add(10);
list.Add(100);
return list;

}




But when I run the application I get OnErrorCall function alert from ajax, how can I solve that? What is problem? thanks every one.

Answer

Make your Method "Static"

that is the only issue in your code

see below

[System.Web.Services.WebMethod]

public static List<int> GetDataBehzad()
{

    List<int> list = new List<int>();
    list.Add(10);
    list.Add(100);
    return list;
}