對Jquery中的ajax再封裝,簡化操作示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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">
$(function () {
jsonAjax("AjaxQuery.aspx", "type=json", "json", callBack);
jsonAjax("AjaxQuery.aspx", "id=1&name=2&type=text", "text", callBackTxt);
});
function callBack(data) {
$("#ddd").html('');
var json = eval(data); //數(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/>");
});
};
function callBackTxt(data) {
$("#ccc").html(data);
};
/**
* ajax post提交
* @param url
* @param param
* @param datat 為html,json,text
* @param callback回調(diào)函數(shù)
* @return
*/
function jsonAjax(url, param, datat, callback) {
$.ajax({
type: "post",
url: url,
data: param,
dataType: datat,
success: callback,
error: function () {
jQuery.fn.mBox({
message: '恢復(fù)失敗'
});
}
});
}
</script>
</head>
<body>
<span id="ccc"></span>
<span id="ddd"></span>
</body>
</html>
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)
{
//數(shù)據(jù)模擬,僅供參考
string messgage = string.Empty;
string id = Request["id"];
string name = Request["name"];
string gettype = Request["type"];
if (gettype=="text")
{
messgage = (id == "1" && name == "2") ? "ok符合條件" : "sorry不符合條件";
}
else if (gettype == "json")
{
List<Student> list = new List<Student>();
for (int i = 0; i < 50; i++)
{
Student a = new Student();
a.Name = "張三" + i;
a.Age = i;
a.Sex = "男";
list.Add(a);
}
messgage = new JavaScriptSerializer().Serialize(list);
}
else
{ }
Response.Write(messgage);
Response.End();
}
}
public struct Student
{
public string Name;
public int Age;
public string Sex;
}
}
相關(guān)文章
firefox下jquery ajax返回object XMLDocument處理方法
使用jquery ajax處理struts2 返回json類型的時候,ajax執(zhí)行成功返回結(jié)果為object XMLDocument,解決方法如下2014-01-01
jQuery的DOM操作之刪除節(jié)點(diǎn)示例
如果文檔中某一個元素多余,那么應(yīng)將其刪除。jQuery提供了兩種刪除節(jié)點(diǎn)的方法,remove()方法和empty()方法下面為大家詳細(xì)介紹下2014-01-01
JQuery使用index方法獲取Jquery對象數(shù)組下標(biāo)的方法
這篇文章主要介紹了JQuery使用index方法獲取Jquery對象數(shù)組下標(biāo)的方法,涉及jQuery中index方法的使用技巧,需要的朋友可以參考下2015-05-05
Jquery網(wǎng)頁出現(xiàn)的亂碼問題的三種解決方法
很多時候,在網(wǎng)上下的一些Jquery插件,在頁面運(yùn)行時出現(xiàn)亂碼問題,我總結(jié)了三點(diǎn),希望對大家有所幫助:2013-06-06
jQuery live( type, fn ) 委派事件實現(xiàn)
jQuery 1.3中新增的方法。給所有當(dāng)前以及將來會匹配的元素綁定一個事件處理函數(shù)(比如click事件)。也能綁定自定義事件。2009-10-10

