简体   繁体   English

如何从asp.net Web表单返回列表对象到ajax回调函数?

[英]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: 我是asp.net的初学者,编写简单的Web应用程序,向用户展示使用highcharts工具的任何图表,在highcharts中需要使用ajax读取数据,为此我编写了这段代码:

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: 并在CS.aspx编写此代码:

[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? 但是当我运行应用程序时,我从ajax获取OnErrorCall函数警报,我该如何解决? What is problem? 有什么问题? thanks every one. 感谢大家。

Let see this is not and complete answer but a direction to a result. 让我们看看这不是完整的答案,而是结果的方向。 Try something like returning Json/Xml result from the action WebMethod. 尝试从动作WebMethod返回Json / Xml结果。 I have tried with mvc it is working on my site. 我试过用mvc它正在我的网站上工作。 You just need to see if JsonResult/Similar something is by default supported in webform or just find a class library. 你只需要看看在webform中是否默认支持JsonResult / Similar某些东西,或者只是找到一个类库。

    public JsonResult   GetDataBehzad()
    {

        List<int> list = new List<int>();
        list.Add(10);
        list.Add(100); 
        return Json(list, JsonRequestBehavior.AllowGet); 

    }

Use [ScriptMethod(ResponseFormat = ResponseFormat.Json)] attribute. 使用[ScriptMethod(ResponseFormat = ResponseFormat.Json)]属性。

Your method: 你的方法:

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

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

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM