X-Git-Url: http://git.asbjorn.biz/?a=blobdiff_plain;f=src%2Fajax.js;h=4f1fc6c42f2026e52d3c197420f66e1995255a87;hb=22e28b01e60e87b2454f88ca128fb84916b13564;hp=3f4f732bd30a2aaaa3c45e172b7741e9a3843fb8;hpb=1f92edee207829a28de80ee72548cdbd599bcc79;p=jquery.git diff --git a/src/ajax.js b/src/ajax.js index 3f4f732..4f1fc6c 100644 --- a/src/ajax.js +++ b/src/ajax.js @@ -1,5 +1,5 @@ (function( jQuery ) { - + var rscript = /)<[^<]*)*<\/script>/gi, rselectTextarea = /^(?:select|textarea)/i, rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i, @@ -43,9 +43,9 @@ jQuery.fn.extend({ type = "POST"; } } - + var self = this; - + // Request the remote document jQuery.ajax({ url: url, @@ -113,9 +113,8 @@ jQuery.each( "ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".sp }; }); -jQuery.extend({ - - get: function( url, data, callback, type ) { +jQuery.each( [ "get", "post" ], function( i, method ) { + jQuery[ method ] = function( url, data, callback, type ) { // shift arguments if data argument was omited if ( jQuery.isFunction( data ) ) { type = type || callback; @@ -124,13 +123,16 @@ jQuery.extend({ } return jQuery.ajax({ - type: "GET", + type: method, url: url, data: data, success: callback, dataType: type }); - }, + }; +}); + +jQuery.extend({ getScript: function( url, callback ) { return jQuery.get(url, null, callback, "script"); @@ -140,23 +142,6 @@ jQuery.extend({ return jQuery.get(url, data, callback, "json"); }, - post: function( url, data, callback, type ) { - // shift arguments if data argument was omited - if ( jQuery.isFunction( data ) ) { - type = type || callback; - callback = data; - data = {}; - } - - return jQuery.ajax({ - type: "POST", - url: url, - data: data, - success: callback, - dataType: type - }); - }, - ajaxSetup: function( settings ) { jQuery.extend( jQuery.ajaxSettings, settings ); }, @@ -181,12 +166,7 @@ jQuery.extend({ xhr: function() { return new window.XMLHttpRequest(); }, - xhrResponseFields: { - xml: "XML", - text: "Text", - json: "JSON" - }, - + accepts: { xml: "application/xml, text/xml", html: "text/html", @@ -194,41 +174,42 @@ jQuery.extend({ json: "application/json, text/javascript", "*": "*/*" }, - + autoDataType: { xml: /xml/, html: /html/, json: /json/ }, - + // Prefilters // 1) They are useful to introduce custom dataTypes (see transport/jsonp for an example) // 2) These are called: // * BEFORE asking for a transport // * AFTER param serialization (s.data is a string if s.processData is true) - // 3) They MUST be order agnostic - prefilters: [], - + // 3) key is the dataType + // 4) the catchall symbol "*" can be used + // 5) execution will start with transport dataType and THEN continue down to "*" if needed + prefilters: {}, + // Transports bindings // 1) key is the dataType // 2) the catchall symbol "*" can be used // 3) selection will start with transport dataType and THEN go to "*" if needed - transports: { - }, - + transports: {}, + // Checkers // 1) key is dataType // 2) they are called to control successful response // 3) error throws is used as error data dataCheckers: { - + // Check if data is a string "text": function(data) { if ( typeof data != "string" ) { jQuery.error("typeerror"); } }, - + // Check if xml has been properly parsed "xml": function(data) { var documentElement = data ? data.documentElement : data; @@ -240,51 +221,36 @@ jQuery.extend({ } } }, - + // List of data converters - // 1) key format is "source_type => destination_type" (spaces required) + // 1) key format is "source_type destination_type" (a single space in-between) // 2) the catchall symbol "*" can be used for source_type dataConverters: { - + // Convert anything to text - "* => text": function(data) { - return "" + data; - }, - + "* text": window.String, + // Text to html (no transformation) - "text => html": function(data) { - return data; - }, - + "text html": window.String, + // Evaluate text as a json expression - "text => json": jQuery.parseJSON, - + "text json": jQuery.parseJSON, + // Parse text as xml - "text => xml": function(data) { - var xml, parser; - if ( window.DOMParser ) { // Standard - parser = new DOMParser(); - xml = parser.parseFromString(data,"text/xml"); - } else { // IE - xml = new ActiveXObject("Microsoft.XMLDOM"); - xml.async="false"; - xml.loadXML(data); - } - return xml; - } + "text xml": jQuery.parseXML } }, // Main method ajax: function( url , s ) { - + if ( arguments.length === 1 ) { s = url; url = s ? s.url : undefined; } - + return jQuery.xhr().open( s ? s.type : undefined , url ).send( undefined , s ); - + }, // Serialize an array of form elements or a set of @@ -296,19 +262,19 @@ jQuery.extend({ 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 ) { // 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. @@ -341,7 +307,7 @@ function buildParams( prefix, obj, traditional, add ) { buildParams( prefix + "[" + ( typeof v === "object" || jQuery.isArray(v) ? i : "" ) + "]", v, traditional, add ); } }); - + } else if ( !traditional && obj != null && typeof obj === "object" ) { // If we see an array here, it is empty and should be treated as an empty // object @@ -354,7 +320,7 @@ function buildParams( prefix, obj, traditional, add ) { buildParams( prefix + "[" + k + "]", v, traditional, add ); }); } - + } else { // Serialize scalar item. add( prefix, obj ); @@ -388,7 +354,7 @@ if ( window.ActiveXObject ) { return new window.XMLHttpRequest(); } catch( xhrError ) {} } - + try { return new window.ActiveXObject("Microsoft.XMLHTTP"); } catch( activeError ) {}