当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net中Jquery解析json数据格式

asp.net中Jquery解析json数据格式

2018年04月14日  | 移动技术网IT编程  | 我要评论

aspx前台页面代码如下:

    <script language="javascript" type="text/javascript">
          function  TestAjax(){
            $.ajax({
                    url: ".com/">www.2cto.com/", //ajax请求的路径
                    dataType: "json",
                    data: "id=150"+"&r=" + Math.random(), //请求的参数
                    type: "post",
                    success: function (data) {
                        alert(data.name); //弹出data对象的name属性值
                    }
                });
          }
    </script>

 

后台.cs文件如下:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
                string id = Request.QueryString["id"].ToString();
                Context.Response.ContentType = "text/plain";
                //构建的json数据(只有一条数据可以直接用{},如果有多条数据需要用[],前台用索引访问) 例如jsonstr = "[{\"pricelist\":\"" + temm + "\",\"jianye\":\"" + jianye + "\",\"sigleprice\":\"" + oneytempprice + "\",\"jianyetotalprice\":\"" + jianyetotalprice + "\"}]";
                string data = "{\"id\":\"" + id + "\",\"name\":\"测试内容 \",\"sigleprice\":\"120.0 \",\"jianyetotalprice\":\"123.2\"}";
                Context.Response.Write(data);
                Context.Response.End();
            }
        }
    }

 作者:zcb888

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网