之前寫過一篇《.NET MVC 異步提交和返回參數》 ,里面有一些ajax的內容,但是不深入,這次詳細剖析下jquery中$.ajax的用法。
首先,上代碼;
jquery-1.5.1 $.ajax源碼:

jQuery.extend({ getScript: function( url, callback ) { return jQuery.get( url, undefined, callback, "script" ); }, getJSON: function( url, data, callback ) { return jQuery.get( url, data, callback, "json" ); }, // Creates a full fledged settings object into target
// with both ajaxSettings and settings fields.
// If target is omitted, writes into ajaxSettings.
ajaxSetup: function ( target, settings ) { if ( !settings ) { // Only one parameter, we extend ajaxSettings
settings = target; target = jQuery.extend( true, jQuery.ajaxSettings, settings ); } else { // target was provided, we extend into it
jQuery.extend( true, target, jQuery.ajaxSettings, settings ); } // Flatten fields we don't want deep extended
for( var field in { context: 1, url: 1 } ) { if ( field in settings ) { target[ field ] = settings[ field ]; } else if( field in jQuery.ajaxSettings ) { target[ field ] = jQuery.ajaxSettings[ field ]; } } return target; }, ajaxSettings: { url: ajaxLocation, isLocal: rlocalProtocol.test( ajaxLocParts[ 1 ] ), global: true, type: "GET", contentType: "application/x-www-form-urlencoded", processData: true, async: true, /* timeout: 0, data: null, dataType: null, username: null, password: null, cache: null, traditional: false, headers: {}, crossDomain: null, */ accepts: { xml: "application/xml, text/xml", html: "text/html", text: "text/plain", json: "application/json, text/javascript", "*": "*/*" }, contents: { xml: /xml/, html: /html/, json: /json/ }, responseFields: { xml: "responseXML", text: "responseText" }, // List of data converters
// 1) key format is "source_type destination_type" (a single space in-between)
// 2) the catchall symbol "*" can be used for source_type
converters: { // Convert anything to text
"* text": window.String, // Text to html (true = no transformation)
"text html": true, // Evaluate text as a json expression
"text json": jQuery.parseJSON, // Parse text as xml
"text xml": jQuery.parseXML } }, ajaxPrefilter: addToPrefiltersOrTransports( prefilters ), ajaxTransport: addToPrefiltersOrTransports( transports ), // Main method
ajax: function( url, options ) { // If url is an object, simulate pre-1.5 signature
if ( typeof url === "object" ) { options = url; url = undefined; } // Force options to be an object
options = options || {}; var // Create the final options object
s = jQuery.ajaxSetup( {}, options ), // Callbacks context
callbackContext = s.context || s, // Context for global events
// It's the callbackContext if one was provided in the options
// and if it's a DOM node or a jQuery collection
globalEventContext = callbackContext !== s && ( callbackContext.nodeType || callbackContext instanceof jQuery ) ? jQuery( callbackContext ) : jQuery.event, // Deferreds
deferred = jQuery.Deferred(), completeDeferred = jQuery._Deferred(), // Status-dependent callbacks
statusCode = s.statusCode || {}, // ifModified key
ifModifiedKey, // Headers (they are sent all at once)
requestHeaders = {}, // Response headers
responseHeadersString, responseHeaders, // transport
transport, // timeout handle
timeoutTimer, // Cross-domain detection vars
parts, // The jqXHR state
state = 0, // To know if global events are to be dispatched
fireGlobals, // Loop variable
i, // Fake xhr
jqXHR = { readyState: 0, // Caches the header
setRequestHeader: function( name, value ) { if ( !state ) { requestHeaders[ name.toLowerCase().replace( rucHeaders, rucHeadersFunc ) ] = value; } return this; }, // Raw string
getAllResponseHeaders: function() { return state === 2 ? responseHeadersString : null; }, // Builds headers hashtable if needed
getResponseHeader: function( key ) { var match; if ( state === 2 ) { if ( !responseHeaders ) { responseHeaders = {}; while( ( match = rheaders.exec( responseHeadersString ) ) ) { responseHeaders[ match[1].toLowerCase() ] = match[ 2 ]; } } match = responseHeaders[ key.toLowerCase() ]; } return match === undefined ? null : match; }, // Overrides response content-type header
overrideMimeType: function( type ) { if ( !state ) { s.mimeType = type; } return this; }, // Cancel the request
abort: function( statusText ) { statusText = statusText || "abort"; if ( transport ) { transport.abort( statusText ); } done( 0, statusText ); return this; } }; // Callback for when everything is done
// It is defined here because jslint complains if it is declared
// at the end of the function (which would be more logical and readable)
function done( status, statusText, responses, headers ) { // Called once
if ( state === 2 ) { return; } // State is "done" now
state = 2; // Clear timeout if it exists
if ( timeoutTimer ) { clearTimeout( timeoutTimer ); } // Dereference transport for early garbage collection
// (no matter how long the jqXHR object will be used)
transport = undefined; // Cache response headers
responseHeadersString = headers || ""; // Set readyState
jqXHR.readyState = status ? 4 : 0; var isSuccess, success, error, response = responses ? ajaxHandleResponses( s, jqXHR, responses ) : undefined, lastModified, etag; // If successful, handle type chaining
if ( status >= 200 && status < 300 || status === 304 ) { // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
if ( s.ifModified ) { if ( ( lastModified = jqXHR.getResponseHeader( "Last-Modified" ) ) ) { jQuery.lastModified[ ifModifiedKey ] = lastModified; } if ( ( etag = jqXHR.getResponseHeader( "Etag" ) ) ) { jQuery.etag[ ifModifiedKey ] = etag; } } // If not modified
if ( status === 304 ) { statusText = "notmodified"; isSuccess = true; // If we have data
} else { try { success = ajaxConvert( s, response ); statusText = "success"; isSuccess = true; } catch(e) { // We have a parsererror
statusText = "parsererror"; error = e; } } } else { // We extract error from statusText
// then normalize statusText and status for non-aborts
error = statusText; if( !statusText || status ) { statusText = "error"; if ( status < 0 ) { status = 0; } } } // Set data for the fake xhr object
jqXHR.status = status; jqXHR.statusText = statusText; // Success/Error
if ( isSuccess ) { deferred.resolveWith( callbackContext, [ success, statusText, jqXHR ] ); } else { deferred.rejectWith( callbackContext, [ jqXHR, statusText, error ] ); } // Status-dependent callbacks
jqXHR.statusCode( statusCode ); statusCode = undefined; if ( fireGlobals ) { globalEventContext.trigger( "ajax" + ( isSuccess ? "Success" : "Error" ), [ jqXHR, s, isSuccess ? success : error ] ); } // Complete
completeDeferred.resolveWith( callbackContext, [ jqXHR, statusText ] ); if ( fireGlobals ) { globalEventContext.trigger( "ajaxComplete", [ jqXHR, s] ); // Handle the global AJAX counter
if ( !( --jQuery.active ) ) { jQuery.event.trigger( "ajaxStop" ); } } } // Attach deferreds
deferred.promise( jqXHR ); jqXHR.success = jqXHR.done; jqXHR.error = jqXHR.fail; jqXHR.complete = completeDeferred.done; // Status-dependent callbacks
jqXHR.statusCode = function( map ) { if ( map ) { var tmp; if ( state < 2 ) { for( tmp in map ) { statusCode[ tmp ] = [ statusCode[tmp], map[tmp] ]; } } else { tmp = map[ jqXHR.status ]; jqXHR.then( tmp, tmp ); } } return this; }; // Remove hash character (#7531: and string promotion)
// Add protocol if not provided (#5866: IE7 issue with protocol-less urls)
// We also use the url parameter if available
s.url = ( ( url || s.url ) + "" ).replace( rhash, "" ).replace( rprotocol, ajaxLocParts[ 1 ] + "//" ); // Extract dataTypes list
s.dataTypes = jQuery.trim( s.dataType || "*" ).toLowerCase().split( rspacesAjax ); // Determine if a cross-domain request is in order
if ( !s.crossDomain ) { parts = rurl.exec( s.url.toLowerCase() ); s.crossDomain = !!( parts && ( parts[ 1 ] != ajaxLocParts[ 1 ] || parts[ 2 ] != ajaxLocParts[ 2 ] || ( parts[ 3 ] || ( parts[ 1 ] === "http:" ? 80 : 443 ) ) != ( ajaxLocParts[ 3 ] || ( ajaxLocParts[ 1 ] === "http:" ? 80 : 443 ) ) ) ); } // Convert data if not already a string
if ( s.data && s.processData && typeof s.data !== "string" ) { s.data = jQuery.param( s.data, s.traditional ); } // Apply prefilters
inspectPrefiltersOrTransports( prefilters, s, options, jqXHR ); // If request was aborted inside a prefiler, stop there
if ( state === 2 ) { return false; } // We can fire global events as of now if asked to
fireGlobals = s.global; // Uppercase the type
s.type = s.type.toUpperCase(); // Determine if request has content
s.hasContent = !rnoContent.test( s.type ); // Watch for a new set of requests
if ( fireGlobals && jQuery.active++ === 0 ) { jQuery.event.trigger( "ajaxStart" ); } // More options handling for requests with no content
if ( !s.hasContent ) { // If data is available, append data to url
if ( s.data ) { s.url += ( rquery.test( s.url ) ? "&" : "?" ) + s.data; } // Get ifModifiedKey before adding the anti-cache parameter
ifModifiedKey = s.url; // Add anti-cache in url if needed
if ( s.cache === false ) { var ts = jQuery.now(), // try replacing _= if it is there
ret = s.url.replace( rts, "$1_=" + ts ); // if nothing was replaced, add timestamp to the end
s.url = ret + ( (ret === s.url ) ? ( rquery.test( s.url ) ? "&" : "?" ) + "_=" + ts : "" ); } } // Set the correct header, if data is being sent
if ( s.data && s.hasContent && s.contentType !== false || options.contentType ) { requestHeaders[ "Content-Type" ] = s.contentType; } // Set the If-Modified-Since and/or If-None-Match header, if in ifModified mode.
if ( s.ifModified ) { ifModifiedKey = ifModifiedKey || s.url; if ( jQuery.lastModified[ ifModifiedKey ] ) { requestHeaders[ "If-Modified-Since" ] = jQuery.lastModified[ ifModifiedKey ]; } if ( jQuery.etag[ ifModifiedKey ] ) { requestHeaders[ "If-None-Match" ] = jQuery.etag[ ifModifiedKey ]; } } // Set the Accepts header for the server, depending on the dataType
requestHeaders.Accept = s.dataTypes[ 0 ] && s.accepts[ s.dataTypes[0] ] ? s.accepts[ s.dataTypes[0] ] + ( s.dataTypes[ 0 ] !== "*" ? ", */*; q=0.01" : "" ) : s.accepts[ "*" ]; // Check for headers option
for ( i in s.headers ) { jqXHR.setRequestHeader( i, s.headers[ i ] ); } // Allow custom headers/mimetypes and early abort
if ( s.beforeSend && ( s.beforeSend.call( callbackContext, jqXHR, s ) === false || state === 2 ) ) { // Abort if not done already
jqXHR.abort(); return false; } // Install callbacks on deferreds
for ( i in { success: 1, error: 1, complete: 1 } ) { jqXHR[ i ]( s[ i ] ); } // Get transport
transport = inspectPrefiltersOrTransports( transports, s, options, jqXHR ); // If no transport, we auto-abort
if ( !transport ) { done( -1, "No Transport" ); } else { jqXHR.readyState = 1; // Send global event
if ( fireGlobals ) { globalEventContext.trigger( "ajaxSend", [ jqXHR, s ] ); } // Timeout
if ( s.async && s.timeout > 0 ) { timeoutTimer = setTimeout( function(){ jqXHR.abort( "timeout" ); }, s.timeout ); } try { state = 1; transport.send( requestHeaders, done ); } catch (e) { // Propagate exception as error if not done
if ( status < 2 ) { done( -1, e ); // Simply rethrow otherwise
} else { jQuery.error( e ); } } } return jqXHR; }, // Serialize an array of form elements or a set of
// key/values into a query string
param: function( a, traditional ) { var s = [], add = function( key, value ) { // If value is a function, invoke it and return its value
value = jQuery.isFunction( value ) ? value() : value; s[ s.length ] = encodeURIComponent( key ) + "=" + encodeURIComponent( value ); }; // Set traditional to true for jQuery <= 1.3.2 behavior.
if ( traditional === undefined ) { traditional = jQuery.ajaxSettings.traditional; } // If an array was passed in, assume that it is an array of form elements.
if ( jQuery.isArray( a ) || ( a.jquery && !jQuery.isPlainObject( a ) ) ) { // Serialize the form elements
jQuery.each( a, function() { add( this.name, this.value ); } ); } else { // If traditional, encode the "old" way (the way 1.3.2 or older
// did it), otherwise encode params recursively.
for ( var prefix in a ) { buildParams( prefix, a[ prefix ], traditional, add ); } } // Return the resulting serialization
return s.join( "&" ).replace( r20, "+" ); } });
Jquery的$.ajax的用法:
jQuery.ajax( options ) : 通過 HTTP 請求加載遠程數據,這個是jQuery 的底層 AJAX 實現。簡單易用的高層實現見 $.get, $.post 等。
$.ajax() 返回其創建的 XMLHttpRequest 對象。大多數情況下你無需直接操作該對象,但特殊情況下可用於手動終止請求。
注意: 如果你指定了 dataType 選項,請確保服務器返回正確的MIME信息,(如xml返回"text/xml")。錯誤的MIME類型可能導致不可預知的錯誤。見Specifying the Data Type for AJAX Requests 。
當設置 datatype 類型為 'script' 的時候,所有的遠程(不在同一個域中)POST請求都回轉換為GET方式。
$.ajax() 只有一個參數:參數 key/value 對象,包含各配置及回調函數信息。詳細參數選項見下。
jQuery 1.2 中,您可以跨域加載 JSON 數據,使用時需將數據類型設置為 JSONP。使用 JSONP 形式調用函數時,如 "myurl?callback=?" jQuery 將自動替換 ? 為正確的函數名,以執行回調函數。數據類型設置為 "jsonp" 時,jQuery 將自動調用回調函數。(這個我不是很懂)
jquery ajax 參數列表:
url(String)
(默認: 當前頁地址) 發送請求的地址。
type(String)
請求方式 (參數有兩種 "POST" 和 "GET"),默認為 "GET"。注意:其它 HTTP 請求方法,如 PUT 和 DELETE 也可以使用,但僅部分瀏覽器支持。
timeout(Number)
設置請求超時時間(毫秒)。此設置將覆蓋全局設置。
async(Boolean)
(默認: true) 設置為true的情況下所有請求均為異步請求。如果需要發送同步請求,請將此選項設置為 false。注意,同步請求將鎖住瀏覽器,用戶其它操作必須等待請求完成才可以執行。
beforeSend(Function)
發送請求前可修改 XMLHttpRequest 對象的函數,如添加自定義 HTTP 頭。XMLHttpRequest 對象是唯一的參數。
function(XMLHttpRequest){ this; // the options for this ajax request }
cache(Boolean)
是否將請求結果設置緩存(默認: true) ,設置為 false 將不會從瀏覽器緩存中加載請求信息,注意開發初期最好設置為false,否則不方便調試的哦。
complete(Function)
請求完成后回調函數 (請求成功或失敗時均調用)。參數: XMLHttpRequest 對象,成功信息字符串。
function(XMLHttpRequest,textStatus){ this;//theoptionsforthisajaxrequest }
contentType(String)
(默認: "application/x-www-form-urlencoded") 發送信息至服務器時內容編碼類型。默認值適合大多數應用場合。
data(Object,String)
發送到服務器的數據。將自動轉換為請求字符串格式。GET 請求中將附加在 URL 后。查看 processData 選項說明以禁止此自動轉換。必須為 Key/Value格式。如果為數組,jQuery 將自動為不同值對應同一個名稱。如 {foo:["bar1", "bar2"]} 轉換為 '&foo=bar1&foo=bar2'。
dataType(String)
定義服務器返回的數據類型。如果不指定,jQuery 將自動根據 HTTP 包 MIME 信息返回 responseXML 或 responseText,並作為回調函數參數傳遞,可用值:
"xml": 返回 XML 格式數據,可用 jQuery 處理。
"html": 返回純文本 HTML 格式數據;可包含 script 元素。
"script": 返回純文本 JavaScript 代碼。不會自動緩存結果。
"json": 返回 JSON 數據 。
"jsonp": JSONP 格式。使用 JSONP 形式調用函數時,如 "myurl?callback=?" jQuery 將自動替換 ? 為正確的函數名,以執行回調函數。
error(Function)
(默認: 自動判斷 (xml 或 html)) 請求失敗時將調用此方法。這個方法有三個參數:XMLHttpRequest 對象,錯誤信息,(可能)捕獲的錯誤對象。
function(XMLHttpRequest,textStatus,errorThrown){ //通常情況下textStatus和errorThown只有其中一個有值 this;//theoptionsforthisajaxrequest }
global(Boolean)
是否觸發全局 AJAX 事件(默認: true) 。設置為 false 將不會觸發全局 AJAX 事件,如 ajaxStart 或 ajaxStop 。可用於控制不同的Ajax事件
ifModified(Boolean)
(默認: false) 僅在服務器數據改變時獲取新數據。使用 HTTP 包 Last-Modified 頭信息判斷。
processData(Boolean)
設置發送數據的信息格式(默認: true),設置為 true 的時候發送的數據將被轉換為對象(技術上講並非字符串) 以配合默認內容類型 "application/x-www-form-urlencoded"。如果要發送 DOM 樹信息或其它不希望轉換的信息,請設置為 false。
success(Function)
請求成功后回調函數。這個方法有兩個參數:服務器返回數據,返回狀態
function(data,textStatus){ //datacouldbexmlDoc,jsonObj,html,text,etc...
this;//theoptionsforthisajaxrequest
}
下面以一則示例解釋一下該方法的具體的用法:
$.ajax({ type:'get', url:'http://www.www.daimajiayuan.com/rss', beforeSend:function(XMLHttpRequest){ //ShowLoading();
}, success:function(data,textStatus){ $('.ajax.ajaxResult').html(''); $('item',data).each(function(i,domEle){ $('.ajax.ajaxResult').append('<li>'+$(domEle).children('title').text()+'</li>'); }); }, complete:function(XMLHttpRequest,textStatus){ //HideLoading();
}, error:function(){ //請求出錯處理
} });
Jquery的$.post的用法:
jQuery.post( url, [data], [callback], [type] ) :使用POST方式來進行異步請求
jquery $.post 方法參數列表(說明):
url (String) : 發送請求的URL地址.
data (Map) : (可選) 要發送給服務器的數據,以 Key/value 的鍵值對形式表示,可將此值放到url中。
callback (Function) : (可選) 載入成功時回調函數(只有當Response的返回狀態是success才能調用該方法)。
type (String) : (可選)客戶端請求的數據類型(JSON,XML,等等)
這是一個簡單的 POST 請求功能以取代復雜 $.ajax ,請求成功時可調用回調函數。如果需要在出錯時執行函數,請使用 $.ajax。
下面是一個使用$.post的簡單示例代碼:
$.post( 'http://www.daimajiayuan.com/ajax.php', {Action:"post",Name:"lulu"}, function(data,textStatus){ //data可以是xmlDoc,jsonObj,html,text,等等.
//this;//這個Ajax請求的選項配置信息,請參考jQuery.get()說到的this
alert(data.result); }, "json"//這里設置了請求的返回格式為"json"
);
如果你設置了請求的格式為"json",此時你沒有設置Response回來的ContentType 為:Response.ContentType = "application/json"; 那么你將無法捕捉到返回的數據。
注意,上面的示例中 alert(data.result); 由於設置了Accept報頭為"json",這里返回的data就是一個對象,因此不需要用eval()來轉換為對象。