JSON - 常用代码

创建时间:
2015-06-11 21:35
最近更新:
2018-10-02 23:06

HttpHandler

public class Handler : IHttpHandler 
{
    public void ProcessRequest (HttpContext context) {
        context.Response.Clear();
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "application/json";
        context.Response.Write(jsonStr);
        context.Response.End();
    }
}

.aspx

protected void Page_Load(object sender, EventArgs e)
{
    dynamic anything;
    string jsonStr = serializer.Serialize(anything);
    Response.Clear();
    Response.ContentType = "application/json";
    Response.Write(jsonStr);
    Response.End();
}

Request[""]

string b = Request["b"];
string a = Request.QueryString["a"];
string c = Request.Form["c"];

$.getJSON()

$.getJSON(url, function (json) {
    //解析json数组用的方法
    $.each(json, function (index, array) { });
});
$.getJSON(url, { IdOfClsTb: val }, function(json) {
    var jOpts = [];
    $.each(json, function(key, val) {
        jOpts.push('<option value="' + key + '">' + val + '</option>');
    });
    var html = jOpts.join('');
});

$.ajax()

 $.ajax({
        url: "test_ajax.aspx/Convert",
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        data: '{ "strJson":' + json + '}',
        success: function (result) {
            alert(result.d);
        },
        error: function () {
            alert("error");
        }
    });