如何调用jQuery的C#方法? [英] How to call c# method in jquery?

查看:137
本文介绍了如何调用jQuery的C#方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是图它应该从C#的输入绘制图表。我使用JSON从C#返回值的jQuery。但它并不能帮助我。提前致谢。能否请你找到我在哪里出了错..?

这是我的aspx code

 脚本类型=文/ JavaScript的>
        $(文件)。就绪(函数(){            无功源= {};
            $阿贾克斯({                输入:POST,
                数据类型:JSON,
                网址:Default.aspx的/ GETALL
                的contentType:应用/ JSON的;字符集= UTF-8,
                缓存:假的,
                成功:函数(响应){                    来源= $ .parseJSON(response.d);                },
                错误:功能(错误){
                    警报('错误');
                }
            });

这是我的CS code

 公共类的sampleData
{
    公共字符串日{搞定;组; }
    公众诠释基思{搞定;组; }
    公众诠释埃里卡{搞定;组; }
    公众诠释乔治{搞定;组; }
}公共部分类_Default:System.Web.UI.Page
{
    清单<&sampleData在GT; S =新的List<&sampleData在GT;();
    保护无效的Page_Load(对象发件人,EventArgs的发送)
    {
        s.Add(新的sampleData {天=星期一,基思= 20,埃里卡= 15,乔治= 25});
        s.Add(新的sampleData {天=星期二,基思= 25,埃里卡= 20,乔治= 35});        会话[数据] = S;    }
    [的WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)
    公共静态列表<&sampleData在GT;得到所有()
    {
        清单<&sampleData在GT;数据=(列表<&sampleData在GT;)HttpContext.Current.Session [数据];
        返回的数据;    }
}


解决方案

我测试了code。一切似乎都很正常,只是我序列化的列表为一个字符串并返回。

  $(窗口).load(函数(){            $阿贾克斯({                键入:POST,
                网址:PageMethodTest.aspx / GETALL
                数据:{},
                的contentType:应用/ JSON的;字符集= UTF-8,
                数据类型:JSON
                成功:fnsuccesscallback,
                错误:fnerrorcallback
            });
        });        功能btnclick(){        }        功能fnsuccesscallback(数据){
            警报(data.d);        }
        功能fnerrorcallback(结果){
            警报(result.statusText);
        }

服务器code:

  [的WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)
        公共静态字符串GETALL()
        {
            清单<&sampleData在GT;数据=(列表<&sampleData在GT;)HttpContext.Current.Session [数据];
            JS的JavaScriptSerializer =新的JavaScriptSerializer();
            返回js.Serialize(数据);
            //返回的数据;        }

和结果

您可以提高使用输出源的图形。

I am using a chart which should get Input from c# for plotting the graph . I am using Json for returning the values from c# to jquery. But it doesn't help me. Thanks in advance. Can you please find where I am went wrong..?

this is my aspx code

script type="text/javascript">
        $(document).ready(function () {

            var source = {};
            $.ajax({

                type: 'POST',
                dataType: 'json',
                url: "Default.aspx/getall",
                contentType: 'application/json; charset=utf-8',
                cache: false,
                success: function (response) {

                    source = $.parseJSON(response.d);

                },
                error: function (err) {
                    alert('Error');
                }
            });

And this is my cs code

public class sampledata
{
    public string Day { get; set; }
    public int Keith { get; set; }
    public int Erica { get; set; }
    public int George { get; set; }


}

public partial class _Default : System.Web.UI.Page 
{
    List<sampledata> s = new List<sampledata>();
    protected void Page_Load(object sender, EventArgs e)
    {
        s.Add(new sampledata { Day = "monday", Keith = 20, Erica = 15, George = 25 });
        s.Add(new sampledata { Day = "tuesday", Keith = 25, Erica = 20, George = 35 });

        Session["data"] = s;            

    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static List<sampledata> getall()
    {
        List<sampledata> data = (List<sampledata>)HttpContext.Current.Session["data"];
        return data;

    }
}

解决方案

I tested your code. Everything seems to be fine, except that i serialized the List into a string and returned.

 $(window).load(function () {

            $.ajax({              

                type: "POST",
                url: "PageMethodTest.aspx/getall",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: fnsuccesscallback,
                error: fnerrorcallback
            });
        });

        function btnclick() {

        }

        function fnsuccesscallback(data) {
            alert(data.d);

        }
        function fnerrorcallback(result) {
            alert(result.statusText);
        }

server code:

[WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static String getall()
        {
            List<sampledata> data = (List<sampledata>)HttpContext.Current.Session["data"];
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Serialize(data);
            //return data;

        }

and the result is

You can improve on using the output as source for your graph..

这篇关于如何调用jQuery的C#方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆