1 ashx文件

using Newtonsoft.Json;(导入空间)

 官方网站:http://www.codeplex.com/Json

public void ProcessRequest (HttpContext context) {

        SelfUser ss = new SelfUser();

        ss.UserName = "宋江";

        ss.UserAge = 30;

       

        context.Response.ContentType = "text/plain";

 

        string strReturn = JsonConvert.SerializeObject(ss);

        context.Response.Write(strReturn);

}

 SelfUser类就两个属性。(2.0做的)

2 前台

//getJSON方法

function getjsonAjax()

{

    $.getJSON(

    "../ajax/json1.ashx",

    function(data)

    {

    alert(data.UserName);

    }

    );

}

//POST方法

 

 

function postAjax()

{   

  $.post(

  "../ajax/json1.ashx",

  {},//此部分须带着

  function(data){

    alert(data.UserName);

  },

  "json"

  );}

注意:注释部分即参数部分须带上,否则不出结果。 

附件:json.net

posted on 2009-11-04 11:24  梅桦  阅读(803)  评论(0编辑  收藏  举报