程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> C#中利用jQuery獲取Json值示例,Ajax方式

C#中利用jQuery獲取Json值示例,Ajax方式

編輯:關於C#
 

<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, //加載執行方法
error: erryFunction, //錯誤執行方法
success: succFunction //成功執行方法
})
function LoadFunction() {
$("#ddd").html('加載中...');
}
function erryFunction() {
alert("error");
}
function succFunction(tt) {
$("#ddd").html('');
var json = eval(tt); //數組
$.each(json, function (index, item) {
//循環獲取數據
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數據" />
<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'); //發布頁面
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, //加載執行方法
error: erryFunction, //錯誤執行方法
success: succFunction //成功執行方法
})
function LoadFunction() {
showLoad("正在運行中...");
}
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("請選擇後再操作");
}
}

 

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));////這個很關鍵,否則error
Response.End();
}
}
public struct Student
{
public string Name;
public int Age;
public string Sex;
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved