jQueryÖ®Ajax·½·¨
1 . jQuery°ïÖúÖ®AjaxÇëÇó£¨Ò»£©jQuery.ajax(options)
Òý×Ô:http://www.flywe.net/article/javascript/jQuery_ajax.html
jQuery.ajax(options)
ͨ¹ý HTTP ÇëÇó¼ÓÔØÔ¶³ÌÊý¾Ý¡£
jQuery µ×²ã AJAX ʵÏÖ¡£¼òµ¥Ò×Óõĸ߲ãʵÏÖ¼û $.get, $.post µÈ¡£
$.ajax() ·µ»ØÆä´´½¨µÄ XMLHttpRequest ¶ÔÏó¡£´ó¶àÊýÇé¿öÏÂÄãÎÞÐèÖ±½Ó²Ù×÷¸Ã¶ÔÏ󣬵«ÌØÊâÇé¿öÏ¿ÉÓÃÓÚÊÖ¶¯ÖÕÖ¹ÇëÇó¡£
$.ajax() Ö»ÓÐÒ»¸ö²ÎÊý£º²ÎÊý key/value ¶ÔÏ󣬰üº¬¸÷ÅäÖü°»Øµ÷º¯ÊýÐÅÏ¢¡£Ïêϸ²ÎÊýÑ¡Ïî¼ûÏ¡£
×¢Ò⣺ Èç¹ûÄãÖ¸¶¨ÁË dataType Ñ¡ÏÇëÈ·±£·þÎñÆ÷·µ»ØÕýÈ·µÄ MIME ÐÅÏ¢£¬(Èç xml ·µ»Ø "text/xml")¡£´íÎóµÄ MIME ÀàÐÍ¿ÉÄܵ¼Ö²»¿ÉÔ¤ÖªµÄ´íÎó¡£¼û Specifying the Data Type for AJAX Requests ¡£
×¢Ò⣺Èç¹ûdataTypeÉèÖÃΪ"script"£¬ÄÇôËùÓеÄÔ¶³Ì(²»ÔÚͬһÓòÃûÏÂ)µÄPOSTÇëÇ󶼽«×ª»¯ÎªGETÇëÇó¡£(ÒòΪ½«Ê¹ÓÃDOMµÄscript±êÇ©À´¼ÓÔØ)
jQuery 1.2 ÖУ¬Äú¿ÉÒÔ¿çÓò¼ÓÔØ JSON Êý¾Ý£¬Ê¹ÓÃʱÐ轫Êý¾ÝÀàÐÍÉèÖÃΪ JSONP¡£Ê¹Óà JSONP ÐÎʽµ÷Óú¯Êýʱ£¬Èç "myurl?callback=?" jQuery ½«×Ô¶¯Ìæ»» ? ΪÕýÈ·µÄº¯ÊýÃû£¬ÒÔÖ´Ðлص÷º¯Êý¡£Êý¾ÝÀàÐÍÉèÖÃΪ "jsonp" ʱ£¬jQuery ½«×Ô¶¯µ÷Óûص÷º¯Êý¡£
·µ»ØÖµ£ºXMLHttpRequest
²ÎÊý£ºoptions (¿ÉÑ¡) : AJAX ÇëÇóÉèÖá£ËùÓÐÑ¡ÏÊÇ¿ÉÑ¡µÄ¡£
Ñ¡Ï
async (Boolean) : (ĬÈÏ: true) ĬÈÏÉèÖÃÏ£¬ËùÓÐÇëÇó¾ùΪÒì²½ÇëÇó¡£Èç¹ûÐèÒª·¢ËÍͬ²½ÇëÇó£¬Ç뽫´ËÑ¡ÏîÉèÖÃΪ false¡£×¢Ò⣬ͬ²½ÇëÇó½«Ëøסä¯ÀÀÆ÷£¬Óû§ÆäËü²Ù×÷±ØÐëµÈ´ýÇëÇóÍê³É²Å¿ÉÒÔÖ´ÐС£
beforeSend (Function) : ·¢ËÍÇëÇóÇ°¿ÉÐÞ¸Ä XMLHttpRequest ¶ÔÏóµÄº¯Êý£¬ÈçÌí¼Ó×Ô¶¨Òå HTTP Í·¡£XMLHttpRequest ¶ÔÏóÊÇΨһµÄ²ÎÊý¡£Ajax ʼþ¡£
³ÌÐò´úÂë
function (XMLHttpRequest) {
this; // µ÷Óñ¾´ÎAJAXÇëÇóʱ´«µ
Ïà¹ØÎĵµ£º
AJAXÖÐÎÄÎÊÌâ·ÖΪÁ½´óÀࣺ
1£©·¢ËÍ·¾¶ÖеIJÎÊýÓÐÖÐÎÄ£¬ÔÚ·þÎñÆ÷¶Î½ÓÊÕ²ÎÊýÖµÊÇÂÒÂë
ÀýÈ磺
var url="a.jsp?name=СÀî"£»
xmlHTTP.open ("post",url,true);
½â¾ö°ì·¨£º
ÀûÓÃjavascriptµÄÌṩµÄescape()»òencodeURI()·½·¨
ÀýÈ磺
¿Í»§¶Ë£º
var url="a.jsp?name=СÀî"£»
url=encode ......
function ajaxFunction()
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
......
ÔÚweb.configÖÐÐÞ¸ÄÐÞ¸ÄÊôÐÔ
1£º<configSections><configSections>ÖмÓÏÂÃæ´úÂë
<configSections>
<sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral ......
Ç°¶Îʱ¼äдJSP£¬Ê¹ÓÃAJAXÒÔPOST·½Ê½Ìá½»Êý¾Ý£¬Èç¹ûÊÇÖÐÎÄ×Ö·ûÌá½»¾Í»áÂÒÂ룬ºóÀ´Ð´ASPʱÓõ½AJAXÒÔPOST·½Ê½Ìá½»Êý¾Ý£¬ÖÐÎÄÒ»ÑùÊÇÂÒÂë¡£ËÑË÷Ò»ÏÂÏà¹Ø×ÊÁÏ£¬ÎÊÌâÓ¦¸ÃÊÇÌá½»Êý¾ÝʱÊÇÒÔUTF-8±àÂëÌá½»£¬ËùÒÔ½ÓÊÕʱÈç¹ûʹÓÃGB2312»òÕßÆäËüÖÐÎıàÂëµÄ»°¾Í»áÂÒÂë¡£
ʹÓÃGET·½Ê½Ìá½»Êý¾ÝµÄʱºò£¬ÖÐÎÄÎÊÌâºÜºÃ½â¾ö£¬setRequestHeader ......