如何从WebMethod读取列表到AJAX回调

时间:2018-09-18 10:32:44

标签: c# asp.net ajax

我在Default.aspx页面中输入了以下代码:

window.feed = function(callback) {
        var Data = JSON.stringify({ columnname: "colonna", inputVal: $("#<%=txtLastID.ClientID%>")[0].value });
        $.ajax({
        type: "POST",
        url: "Default.aspx/RefreshChartEcg",
        //data: '{name: "' + $("#<%=txtLastID.ClientID%>")[0].value + '" }',
        //data : '{lastID : "10"}',
        data : Data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            /*var tick = {};
            tick.plot0 = response.d;
            tick.plot1 = parseInt(10 + 90 * Math.random(), 10);
            callback(JSON.stringify(tick));*/
            alert(response.d);
        },
        failure: function(response) {
            alert(response.d);
        }
    });

这是webMethod的代码:

[WebMethod]
        public static Array RefreshChartEcg(string columnname, string inputVal)
        {
            /**
             * qui devo recuperare il codice per recuperare le informazioni real time dal database
             * */
             int lastID=0;
                if (inputVal != "")
                    lastID = Int32.Parse(inputVal);
             Array list = getActivitiesExecution(lastID: lastID).ToArray();
            return list;
        }

此方法,返回CARTELLA_CLINICA_VALORI_ECG对象的列表。

现在如何从AJAX函数的onSuccess方法读取此值?

1 个答案:

答案 0 :(得分:0)

这主要是关于服务器端的,

1)首先,确保将CARTELLA_CLINICA_VALORI_ECG对象可序列化为Json String。

2)定义web方法返回字符串(Json序列化)

[WebMethod]
public static string RefreshChartEcg(string columnname, string inputVal)
{
     ....
     Array list = getActivitiesExecution(lastID: lastID).ToArray();
     var sRet = {YourSerilizationHelper}.Serialize(list);
     return sRet;
}

you need your own serializer to do this according to your requirements.

3)这是我为序列化为json对象而创建的助手。

public class JsonHelper
{

    public static string Serialize(Object data)
    {
        return Serialize(data,Encoding.UTF8);
    }
    public static string Serialize(Object data, Encoding encoding)
    {
        System.Runtime.Serialization.Json.DataContractJsonSerializer serializer  = new System.Runtime.Serialization.Json.DataContractJsonSerializer(data.GetType());
        using(System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            serializer.WriteObject(ms, data);
            return encoding.GetString(ms.ToArray());
        }
    }


    public static Object Deserialize(Type t, string sJsonText)
    {
        return Deserialize(t,sJsonText , Encoding.UTF8);
    }
    public static Object Deserialize(Type t, string sJsonText,Encoding encoding)
    {

            byte[] byteArray = encoding.GetBytes(sJsonText);
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray))
            {
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(t);
                return serializer.ReadObject(ms);
            }

    }
}

希望有帮助。