jQuery+ajax+asp.net獲取Json值的方法
本文實(shí)例講述了jQuery+ajax+asp.net獲取Json值的方法。分享給大家供大家參考,具體如下:
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>jQueryAjaxJson取值示例</title> <script type="text/javascript" src="Scripts/jquery-1.4.4.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#Button1").click(function () { $.ajax({ url: 'AjaxQuery.aspx', type: 'GET', dataType: 'json', timeout: 1000, cache: false, beforeSend: LoadFunction, //加載執(zhí)行方法 error: erryFunction, //錯(cuò)誤執(zhí)行方法 success: succFunction //成功執(zhí)行方法 }) function LoadFunction() { $("#ddd").html('加載中...'); } function erryFunction() { alert("error"); } function succFunction(tt) { $("#ddd").html(''); var json = eval(tt); //數(shù)組 $.each(json, function (index, item) { //循環(huán)獲取數(shù)據(jù) var name = json[index].Name; var age = json[index].Age; var sex = json[index].Sex; $("#ddd").html($("#ddd").html() + "<br>" + name + " - " + age + " - " + sex + "<br/>"); }); } }); }) </script> </head> <body> <input type="button" id="Button1" value="獲取json數(shù)據(jù)" /> <span id="ddd"></span> </body> </html>
//Ajax Post Text function savedata(tempid) { var tid = $('#hidtemplate').attr('value'); var desc = $("#contentdiv").html(); var num_iid = $("#num_iidArr").attr('value'); var num_iid2 = $("#num_iidArr001").attr('value'); //發(fā)布頁(yè)面 var topsvalue = $("#tops").attr('value'); if (num_iid != "" && num_iid2 != "") { $.ajax({ url: 'TabBaoHandler.ashx', type: 'POST', data: 'type=3&num_iid=' + num_iid2 + '&tid=' + tid + '&desc=' + desc + '&top_session=' + topsvalue, dataType: 'text', timeout: 20000, cache: false, //async: false, //同步 beforeSend: LoadFunction, //加載執(zhí)行方法 error: erryFunction, //錯(cuò)誤執(zhí)行方法 success: succFunction //成功執(zhí)行方法 }) function LoadFunction() { showLoad("正在運(yùn)行中..."); } function erryFunction() { $("#contentdiv").html("<p style=\"padding:5px\"><img src=\"images/error.png\" />sorry,提交失敗</p>"); closeLoad(); } function succFunction(tt) { closeLoad(); $("#contentdiv").show().html(tt); } } else { alert("請(qǐng)選擇后再操作"); } }
using System; //新增 using System.Web.Script.Serialization; using System.Collections.Generic; public partial class AjaxQuery : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { List<Student> list = new List<Student>(); Student c = new Student(); c.Name = "張三"; c.Age = 23; c.Sex = "男"; list.Add(c); Student cc = new Student(); cc.Name = "李四"; cc.Age = 25; cc.Sex = "男"; list.Add(cc); Student ccc = new Student(); ccc.Name = "李玲"; ccc.Age = 25; ccc.Sex = "女"; list.Add(ccc); Response.ContentType = "application/json"; Response.Write(new JavaScriptSerializer().Serialize(list));////這個(gè)很關(guān)鍵,否則error Response.End(); } } public struct Student { public string Name; public int Age; public string Sex; } }
PS:這里推薦幾款本站的json格式數(shù)據(jù)在線(xiàn)操作工具供大家免費(fèi)使用,相信在以后的開(kāi)發(fā)中可以派上用場(chǎng):
在線(xiàn)JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
http://tools.jb51.net/code/json
在線(xiàn)XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
C語(yǔ)言風(fēng)格/HTML/CSS/json代碼格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json
json代碼在線(xiàn)格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《jquery中Ajax用法總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery拖拽特效與技巧總結(jié)》、《jQuery擴(kuò)展技巧總結(jié)》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jQuery動(dòng)畫(huà)與特效用法總結(jié)》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- 使用jQuery向asp.net Mvc傳遞復(fù)雜json數(shù)據(jù)-ModelBinder篇
- 使用ASP.NET一般處理程序或WebService返回JSON的實(shí)現(xiàn)代碼
- asp.net(C#)解析Json的類(lèi)代碼
- asp.net JSONHelper JSON幫助類(lèi)
- Jquery 組合form元素為json格式,asp.net反序列化
- asp.net+jquery Jsonp使用方法
- Jquery中g(shù)etJSON在asp.net中的使用說(shuō)明
- ASP.NET JSON字符串與實(shí)體類(lèi)的互轉(zhuǎn)換示例代碼
- 解決Asp.net Mvc返回JsonResult中DateTime類(lèi)型數(shù)據(jù)格式問(wèn)題的方法
- asp.net使用DataTable構(gòu)造Json字符串的方法
- ASP.NET中XML轉(zhuǎn)JSON的方法實(shí)例
- asp.net提取多層嵌套json數(shù)據(jù)的方法
相關(guān)文章
jQuery網(wǎng)頁(yè)右側(cè)廣告跟隨滾動(dòng)代碼分享
這篇文章主要介紹了兩種jQuery實(shí)現(xiàn)網(wǎng)頁(yè)右側(cè)廣告跟隨滾動(dòng)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-10-10跟著JQuery API學(xué)Jquery 之三 篩選
跟著JQuery API學(xué)Jquery 之三 篩選,相當(dāng)于條件判斷類(lèi)型。2010-04-04來(lái)自國(guó)外的14個(gè)圖片放大編輯的jQuery插件整理
這里就是基于jQuery 的14個(gè)圖片放大編輯插件。需要的朋友可以參考下。2010-10-10Easyui筆記2:實(shí)現(xiàn)datagrid多行刪除的示例代碼
本篇文章主要介紹了Easyui筆記2:實(shí)現(xiàn)datagrid多行刪除,詳細(xì)介紹了完成一個(gè)多行勾選并刪除的功能。有興趣的可以了解一下。2017-01-01jQuery實(shí)現(xiàn)的簡(jiǎn)單獲取索引功能示例
這篇文章主要介紹了jQuery實(shí)現(xiàn)的簡(jiǎn)單獲取索引功能,涉及jQuery事件響應(yīng)及元素索引獲取相關(guān)操作技巧,需要的朋友可以參考下2018-06-06