$.post() $.get() $.getJSON()

HTML+JS代码

<html><head><title></title><script src="jquery-2.1.3.js"></script></head><body><div id="UserName"></div><div id="Age"></div><div id="Gender"></div></body></html><script type="text/javascript">//$.post(url,[data],[callback],[type]) 1:地址 2:参数 3:回调函数 4:请求完成后返回的数据类型$.post("Handler1.ashx", function (data) {data = $.parseJSON(data);$("#Gender").text(data.JsonData[2].Gender);}) //因为这里没有设置第四个参数,所以data仅仅是一个字符串,那么我们就需要利用$.parseJSON()方法将data字符串转换成json对象//$.get(url,[data],[callback],[type]) 1:地址 2:参数 3:回调函数 4:请求完成后返回的数据类型$.get("Handler1.ashx", function (data) {console.info("data", data); //输出:{ "JsonData": [{ "UserName": "张三"},{ "Age": "25" },{ "Gender": "男" }]}$("#UserName").text(data.JsonData[0].UserName);}, "json"); //这个"json"是设置获取数据的类型,,所以得到的数据格式为json类型的(可选参数)//$.getJSON(url,[data],[callback]) 1:地址 2:参数 3:回调函数$.getJSON("Handler1.ashx", function (data) {$("#Age").text(data.JsonData[1].Age); //无需设置,$.getJSON获取的数据类型为直接为json,})</script>

一般处理程序

using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace WebApplication1{/// <summary>/// Handler1 的摘要说明/// </summary>public class Handler1 : IHttpHandler{public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/html";var Json = "{\&;JsonData\&;:[{\&;UserName\&;:\&;张三\&;},{\&;Age\&;:\&;25\&;},{\&;Gender\&;:\&;男\&;}]}";context.Response.Write(Json);}public bool IsReusable{get{return false;}}}}

自己要先看得起自己,别人才会看得起你

$.post() $.get() $.getJSON()

相关文章:

你感兴趣的文章:

标签云: