使用jQuery輕松實(shí)現(xiàn)Ajax的實(shí)例代碼
更新時(shí)間:2010年08月16日 15:39:17 作者:
在Asp.Net的MVC架構(gòu)中使用jQuery是一件很容易的事情,而使用jQuery實(shí)現(xiàn)Ajax更加簡(jiǎn)單。
生成Asp.Net MVC框架后,已經(jīng)包含了jQuery腳本,相關(guān)環(huán)境設(shè)置可參看我的另一篇文章:Asp.Net MVC實(shí)例。這里,我們?nèi)匀唤柚鷮?shí)例中的環(huán)境。在生成的框架中的Scripts文件夾中已經(jīng)可以看到j(luò)Query的腳本。
我們?cè)赥estModel.cs中創(chuàng)建一個(gè)函數(shù),以取得Json數(shù)據(jù),仍然使用Tets表,包含兩個(gè)字段:Id和Name。
//JsonDataArray
public static Array GetJsonArray(String name)
{
Array data = null;
try
{
data = (from c in testDb.test
where c.name.Contains(name)
select new { c.id, c.name }).ToArray();
}catch(ArgumentNullException ae)
{}
return data;
}
Json數(shù)據(jù),簡(jiǎn)單來(lái)說(shuō),即使用Key-Value數(shù)組形式的數(shù)據(jù)。然后按默認(rèn)選項(xiàng)創(chuàng)建一個(gè)控制器,生成的控制器只有一個(gè)方法:Index()。我們?cè)賱?chuàng)建一個(gè)方法,以供jQuery調(diào)用。完成的代碼如下:JQueryController.cs。注意:在MVC2.0中默認(rèn)情況中禁止jQuery調(diào)用服務(wù)器數(shù)據(jù),所以必須在代碼中增加訪問(wèn)權(quán)限:JsonRequestBehavior.AllowGet。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
namespace MvcWeb.Controllers
{
public class JQueryController : Controller
{
//
// GET: /JQuery/
public ActionResult Index()
{
return View();
}
public JsonResult FindByName(string name)
{
return Json(TestModel.GetJsonArray(name), JsonRequestBehavior.AllowGet);
}
}
}
然后在Index()上按右鍵,按默認(rèn)選項(xiàng)生成一個(gè)視圖,可在Views/JQuery看到生成的代碼:Index.aspx,生成的代碼非常簡(jiǎn)單,我們?cè)俨迦隨cript代碼,完成如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
JQuery
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#updater').hide();
$('#dataHead').hide();
$('#linkFind').click(function(event) {
event.preventDefault();
$('#dataHead').show();
$('#updater').show();
$.getJSON('/JQuery/FindByName/', { name: $('#textSearch')[0].value }, function(data) {
$('#testList > div').remove();
$.each(data, function(i, item) {
$('#testList').append('<div>' + item.id + ' ' + item.name + '</div>');
});
});
$('#updater').hide();
});
});
</script>
<h2>使用jQuery實(shí)現(xiàn)Ajax實(shí)例</h2>
<div id="query"><%= Html.TextBox("textSearch") %> <a href="#" id="linkFind">搜索</a>
<span class="update" id="updater"> Loading... </span></div>
<div id="dataHead" >ID Name</div>
<div id="testList"></div>
</asp:Content>
運(yùn)行項(xiàng)目,在文本框中輸入“t”,按“搜索”,在頁(yè)面沒(méi)有刷新的情況下顯示出查詢到的數(shù)據(jù),如下圖:

另外,在Ajax開(kāi)發(fā)中,還可以使用Ajax的基礎(chǔ)函數(shù)$.ajax進(jìn)行調(diào)試,當(dāng)出現(xiàn)錯(cuò)誤時(shí),可以打印錯(cuò)誤信息。例如,對(duì)上述的調(diào)用可以用下面代碼調(diào)試:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#linkFind').click(function(event) {
event.preventDefault();
var handleData = function(data) { alert("success:" + data); }
var handleErr = function(e) {
alert(e.responseText);
}
$.ajax({
type: "get",
url: "/Jquery/FindByName",
data: "name=t",
success: handleData,
error: handleErr
});
});
});
</script>
我們?cè)赥estModel.cs中創(chuàng)建一個(gè)函數(shù),以取得Json數(shù)據(jù),仍然使用Tets表,包含兩個(gè)字段:Id和Name。
復(fù)制代碼 代碼如下:
//JsonDataArray
public static Array GetJsonArray(String name)
{
Array data = null;
try
{
data = (from c in testDb.test
where c.name.Contains(name)
select new { c.id, c.name }).ToArray();
}catch(ArgumentNullException ae)
{}
return data;
}
Json數(shù)據(jù),簡(jiǎn)單來(lái)說(shuō),即使用Key-Value數(shù)組形式的數(shù)據(jù)。然后按默認(rèn)選項(xiàng)創(chuàng)建一個(gè)控制器,生成的控制器只有一個(gè)方法:Index()。我們?cè)賱?chuàng)建一個(gè)方法,以供jQuery調(diào)用。完成的代碼如下:JQueryController.cs。注意:在MVC2.0中默認(rèn)情況中禁止jQuery調(diào)用服務(wù)器數(shù)據(jù),所以必須在代碼中增加訪問(wèn)權(quán)限:JsonRequestBehavior.AllowGet。
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcWeb.Models;
namespace MvcWeb.Controllers
{
public class JQueryController : Controller
{
//
// GET: /JQuery/
public ActionResult Index()
{
return View();
}
public JsonResult FindByName(string name)
{
return Json(TestModel.GetJsonArray(name), JsonRequestBehavior.AllowGet);
}
}
}
然后在Index()上按右鍵,按默認(rèn)選項(xiàng)生成一個(gè)視圖,可在Views/JQuery看到生成的代碼:Index.aspx,生成的代碼非常簡(jiǎn)單,我們?cè)俨迦隨cript代碼,完成如下:
復(fù)制代碼 代碼如下:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
JQuery
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#updater').hide();
$('#dataHead').hide();
$('#linkFind').click(function(event) {
event.preventDefault();
$('#dataHead').show();
$('#updater').show();
$.getJSON('/JQuery/FindByName/', { name: $('#textSearch')[0].value }, function(data) {
$('#testList > div').remove();
$.each(data, function(i, item) {
$('#testList').append('<div>' + item.id + ' ' + item.name + '</div>');
});
});
$('#updater').hide();
});
});
</script>
<h2>使用jQuery實(shí)現(xiàn)Ajax實(shí)例</h2>
<div id="query"><%= Html.TextBox("textSearch") %> <a href="#" id="linkFind">搜索</a>
<span class="update" id="updater"> Loading... </span></div>
<div id="dataHead" >ID Name</div>
<div id="testList"></div>
</asp:Content>
運(yùn)行項(xiàng)目,在文本框中輸入“t”,按“搜索”,在頁(yè)面沒(méi)有刷新的情況下顯示出查詢到的數(shù)據(jù),如下圖:

另外,在Ajax開(kāi)發(fā)中,還可以使用Ajax的基礎(chǔ)函數(shù)$.ajax進(jìn)行調(diào)試,當(dāng)出現(xiàn)錯(cuò)誤時(shí),可以打印錯(cuò)誤信息。例如,對(duì)上述的調(diào)用可以用下面代碼調(diào)試:
復(fù)制代碼 代碼如下:
<script language="javascript" type="text/javascript">
$(document).ready(function() {
$('#linkFind').click(function(event) {
event.preventDefault();
var handleData = function(data) { alert("success:" + data); }
var handleErr = function(e) {
alert(e.responseText);
}
$.ajax({
type: "get",
url: "/Jquery/FindByName",
data: "name=t",
success: handleData,
error: handleErr
});
});
});
</script>
相關(guān)文章
基于jquery實(shí)現(xiàn)拆分姓名的方法(純JS版)
jquery拆分姓名處理程序如下,純js實(shí)現(xiàn)的,感興趣的朋友可以參考下哈,希望對(duì)你有所幫助2013-05-05
jQuery模擬爆炸倒計(jì)時(shí)功能實(shí)例代碼
本文通過(guò)代碼給大家介紹了jQuery模擬爆炸倒計(jì)時(shí)功能實(shí)例代碼,非常不錯(cuò),代碼簡(jiǎn)單易懂,需要的朋友參考下吧2017-08-08
基于jQuery實(shí)現(xiàn)動(dòng)態(tài)數(shù)字展示效果
Jq數(shù)據(jù)列表動(dòng)態(tài)效果,動(dòng)態(tài)更新,支持Ajax動(dòng)態(tài)刷新。下面小編給大家介紹下基于jQuery實(shí)現(xiàn)動(dòng)態(tài)數(shù)字展示效果,需要的朋友可以參考下2015-08-08
jQuery getJSON()+.ashx 實(shí)現(xiàn)分頁(yè)(改進(jìn)版)
參考了上一篇Asp .net +jquery +.ashx 文件實(shí)現(xiàn)分頁(yè)并作了改進(jìn):ashx返回json數(shù)據(jù),減少傳輸數(shù)據(jù)量,html頁(yè)面樣式控制也比較靈活,感興趣的朋友可以參考下哈2013-03-03
jquery easyui的tabs使用時(shí)的問(wèn)題
相信很多人用過(guò)jquery easyui,這個(gè)東西非常好用,界面也很美觀,你都不需要在界面上花太多的工夫,例子程序也比較完善,基本上看下例子就能很好的使用easyui了,很方便。2010-03-03
jquery實(shí)現(xiàn)用戶登陸界面(示例講解)
下面小編就為大家?guī)?lái)一篇jquery實(shí)現(xiàn)用戶登陸界面(示例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09
jQuery中的一些常見(jiàn)方法小結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇jQuery中的一些常見(jiàn)方法小結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Jquery動(dòng)態(tài)更改一張位圖的src與Attr的使用
用Jquery想動(dòng)態(tài)更改一張位圖的src發(fā)現(xiàn)css不好用而attr貌似是能操作所有屬性,包括Jquery未封裝的屬性,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-07-07

