$.post() $.get() $.getJSON()
來(lái)源:程序員人生 發(fā)布時(shí)間:2015-04-20 08:26:02 閱讀次數(shù):3533次
HTML+JS代碼
<html>
<head>
<title></title>
<script src="
jquery⑵.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:參數(shù) 3:回調(diào)函數(shù) 4:要求完成后返回的數(shù)據(jù)類型
$.post("Handler1.ashx", function (data) {
data = $.parseJSON(data);
$("#Gender").text(data.JsonData[2].Gender);
}) //由于這里沒有設(shè)置第4個(gè)參數(shù),所以data僅僅是1個(gè)字符串,那末我們就需要利用$.parseJSON()方法將data字符串轉(zhuǎn)換成json對(duì)象
//$.get(url,[data],[callback],[type]) 1:地址 2:參數(shù) 3:回調(diào)函數(shù) 4:要求完成后返回的數(shù)據(jù)類型
$.get("Handler1.ashx", function (data) {
console.info("data", data); //輸出:{ "JsonData": [{ "UserName": "張3"},{ "Age": "25" },{ "Gender": "男" }]}
$("#UserName").text(data.JsonData[0].UserName);
}, "json"); //這個(gè)"json"是設(shè)置獲得數(shù)據(jù)的類型,所以得到的數(shù)據(jù)格式為json類型的(可選參數(shù))
//$.getJSON(url,[data],[callback]) 1:地址 2:參數(shù) 3:回調(diào)函數(shù)
$.getJSON("Handler1.ashx", function (data) {
$("#Age").text(data.JsonData[1].Age); //無(wú)需設(shè)置,$.getJSON獲得的數(shù)據(jù)類型為直接為json,
})
</script>
1般處理程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
/// <summary>
/// Handler1 的摘要說(shuō)明
/// </summary>
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
var Json = "{"JsonData":[{"UserName":"張3"},{"Age":"25"},{"Gender":"男"}]}";
context.Response.Write(Json);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)