jQueryµ÷ÓÃAsp.net AjaxÒ³ÃæÖеķ½·¨
ÔÀ´ÓÃjQueryµÄajax·½Ê½µ÷ÓÃasp.netÒ³Ãæ»ù±¾¶¼Êǵ÷Óõ¥¸öÒ³Ãæ£¬Óɵ÷ÓÃÒ³ÃæResponseÄÚÈÝ£¬¶øÏÖÔÚ²ÉÓÃasp.net ajaxºó£¬ÎÒÃÇÔò¿ÉÒÔ¸üÍêÃÀµÄʹÓÃjQueryºÍasp.net½áºÏÁË£¬´úÂëÈçÏ£º
<asp:ScriptManager ID="ScriptManager1" runat="server" >
<Scripts>
<asp:ScriptReference Path="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" ScriptMode="Release" />
</Scripts>
</asp:ScriptManager>
<div id="Result">Click here.</div>
<script language="javascript">
$(document).ready(function() {
$("#Result").click(function() {
$.ajax({
type: "POST",
url: "WebForm1.aspx/GetDate",//×¢Òâµ÷Ó÷½Ê½£¬Í¬Ñù·½Ê½¿ÉÒÔµ÷ÓÃwebservice
data: {}, //ÔÚÕâÀï¿ÉÒÔÉèÖÃÐèÒª´«µÝµÄ²ÎÊý
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Ìæ»»·µ»ØÄÚÈÝ
$("#Result").text(msg);
},
error: function(xhr,msg,e) { alert(msg);}
});
});
});
</script>
WebForm1.aspx(Ò³ÃæGetDate·½·¨±ØÐëʹÓþ²Ì¬·½·¨²¢ÇÒʹÓÃ[System.Web.Services.WebMethod]ÊôÐÔ£¬Èçµ÷ÓÃwebserviceÖеķ½·¨²»ÐèÒª¾²Ì¬)
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string GetDate()
{
return DateTime.Now.ToString();
}
}
×¢£º´ø²ÎÊýʱ
1¡¢ÉèÖÃ$.ajaxÖеÄdata£¬È磺data: “{'fname':'Freeze', 'lname':'Soul'}”,×¢ÒâÍâΧ˫ÒýºÅ
2¡¢ÉèÖÃ$.ajaxÖеÄbeforeSend£¬È磺
beforeSend: function(xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");},
3¡¢È»ºóÐÞ¸ÄGetDate()·½·¨ÎªGetDate(String frname,String lname)
Ïà¹ØÎĵµ£º
ÆäʵËùνµÄα¾²Ì¬Ò³Ãæ,¾ÍÊÇÖ¸µÄURLÖØÐ´.
1.Ê×ÏÈÔÚweb.configÀïд
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter"/>
</configSections>
2.ÔÚweb.configÀïÌí¼ÓÒÔϽڵã
<httpHandlers>
< ......
ÓкܶàÀíÓÉÈ¥½âÊÍÀí½âASP.NETÒ³ÃæÉúÃüÖÜÆÚÊǷdz£ÖØÒªµÄ£¬Ö÷ÒªÊÇҪȥÀí½âʲôµØ·½·ÅÖÃÊ²Ã´ÌØ¶¨µÄ·½·¨£¬Ê²Ã´Ê±ºòÎÒÃÇÓ¦¸ÃÉèÖÃʲôÏà¹ØµÄÊôÐÔ¡£Èç¹ûÈ¥¿ª·¢×Ô¶¨ÒåµÄ·þÎñÆ÷¿Ø¼þ£¬Àí½âÉúÃüÖÜÆÚ¶Ô¾ÀÕý¿Ø¼þ³õʼ»¯Ê±ºòµÄ´íÎó£¬ÒÔ¼°Ê¹ÓÃview-stateºÍºǫ́´úÂëÉèÖÃÊôÐÔÊǷdz£ÓÐÓõġ££¨¿Ø¼þʼþÖ»ÓëASP.NETÒ³ÃæÏà¹Ø£©
&nb ......
½éÉÜ£º
ÔÚÍøÒ³Ó¦ÓóÌÐòµÄ¿ª·¢ÖУ¬Ò³ÃæÖ®¼äµÄ´«ÖµÓ¦¸ÃÊÇ×î³£¼ûµÄÎÊÌâÁË¡£ ÔÚÕâÆªÎÄÕÂÀazamsharp ½«ÎªÎÒÃǽéÉÜһЩASP.NETÒ³Ãæ´«ÖµµÄ·½Ê½¡£±¾ÎÄËù¾ÙµÄÀý×ӷdz£¼òµ¥£¬½ö½ö°üº¬ÁËÒ»¸öÎı¾¿òºÍ¼¸¸ö°´Å¥£¬µ±°´Å¥µã»÷ʱÎı¾¿òÖеÄ×Ö·û´®½«»áÒÔ²»Í¬µÄ·½Ê½´«µÝµ½ÁíÍâµÄÒ³ÃæÈ¥¡£
1. Response.Redirect £¨» ......
Ðí¶à³ÌÐòÔ±ÔÚ×öÒµÎñ¿ª·¢Ê±ÍùÍù»áÔÚ·þÎñÆ÷¶Ë×öÓû§ÐÅÏ¢µÄÑéÖ¤£¬ÓÐûÓп¼ÂǹýÓÃjqueryµÄajax·½·¨À´ÑéÖ¤µÇÂ½ÄØ£¿ÇÒЧ¹û±ÈÔÚ·þÎñÆ÷¶Ëд´úÂëÀ´ÑéÖ¤ºÃµÄ¶à£¬Ò³ÃæÎÞˢм´¿ÉʵÏÖʵÏֵǽÑéÖ¤£¬´úÂëÒ²¼òµ¥¡£
ÏÖÔÚÏÂÃæÌù³öÀ´µÄÊǺܼòµ¥µÄÓÃjqueryµÄajax·½·¨À´ÑéÖ¤µÇ½µÄ´úÂ룬ÊʺϸսӴ¥jqueryµÄÅóÓÑѧϰ¡£
Ç°Ì¨Ò³Ãæ´úÂ룺
<he ......
Ö±½Ó»ñµÃÒ³Ãæ²ÎÊý£º
if(!IsPostBack) { //ÅжÏÊÇ·ñÊǵÚÒ»´Î¼ÓÔØ´°Ìå
}
if(Page.PreviousPage!=null){ // ÅжÏÉÏÒ»Ò³ÃæµÄ°´Å¥ÊÇ· ......