When serializing text, encode all line breaks as CRLF pairs per the application/x...
[jquery.git] / src / ajax.js
index 9f8e238..8670900 100644 (file)
@@ -51,9 +51,9 @@ jQuery.fn.extend({
                                type = "POST";
                        }
                }
-               
+
                var self = this;
-               
+
                // Request the remote document
                jQuery.ajax({
                        url: url,
@@ -107,9 +107,9 @@ jQuery.fn.extend({
                                null :
                                jQuery.isArray(val) ?
                                        jQuery.map( val, function(val, i){
-                                               return {name: elem.name, value: val};
+                                               return {name: elem.name, value: val.replace(/\r?\n/g, "\r\n")};
                                        }) :
-                                       {name: elem.name, value: val};
+                                       {name: elem.name, value: val.replace(/\r?\n/g, "\r\n")};
                }).get();
        }
 });
@@ -121,9 +121,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;
@@ -132,13 +131,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");
@@ -148,23 +150,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 );
        },
@@ -198,7 +183,7 @@ jQuery.extend({
                        "*": "*/*"
                },
 
-               autoDataType: {
+               contents: {
                        xml: /xml/,
                        html: /html/,
                        json: /json/
@@ -223,7 +208,7 @@ jQuery.extend({
                // 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
-               dataConverters: {
+               converters: {
 
                        // Convert anything to text
                        "* text": window.String,
@@ -408,12 +393,12 @@ jQuery.extend({
                                                        current,
                                                        prev,
                                                        checker,
+                                                       conv,
                                                        conv1,
                                                        conv2,
-                                                       oneConv,
                                                        convertion,
                                                        dataTypes = s.dataTypes,
-                                                       dataConverters = s.dataConverters,
+                                                       converters = s.converters,
                                                        responses = {
                                                                "xml": "XML",
                                                                "text": "Text"
@@ -430,33 +415,38 @@ jQuery.extend({
                                                        
                                                        if ( i ) {
                                                                
+                                                               prev = dataTypes[ i - 1 ];
+                                                               
                                                                if ( prev !== "*" && current !== "*" && prev !== current ) {
                                                                
-                                                                       oneConv = conv1 = 
-                                                                               dataConverters[ ( conversion = prev + " " + current ) ] ||
-                                                                               dataConverters[ "* " + current ];
+                                                                       conv = converters[ ( conversion = prev + " " + current ) ] ||
+                                                                               converters[ "* " + current ];
                                                                        
-                                                                       if ( oneConv !== true ) {
-                                                                               
-                                                                               if ( ! oneConv && prev !== "text" && current !== "text" ) {
-                                                                                       conv1 = dataConverters[ prev + " text" ] || dataConverters[ "* text" ];
-                                                                                       conv2 = dataConverters[ "text " + current ];
-                                                                               }
-                                                                               
-                                                                               if ( oneConv || conv1 && conv2 ) {
-                                                                                       response = oneConv ? conv1( response ) : conv2( conv1( response ) );
-                                                                               } else {
-                                                                                       throw "no " + conversion;
+                                                                       conv1 = conv2 = 0;
+                                                                       
+                                                                       if ( ! conv && prev !== "text" && current !== "text" ) {
+                                                                               conv1 = converters[ prev + " text" ] || converters[ "* text" ];
+                                                                               conv2 = converters[ "text " + current ];
+                                                                               if ( conv1 === true ) {
+                                                                                       conv = conv2;
+                                                                               } else if ( conv2 === true ) {
+                                                                                       conv = conv1;
                                                                                }
                                                                        }
+                                                                       
+                                                                       if ( ! ( conv || conv1 && conv2 ) ) {
+                                                                               throw conversion;
+                                                                       }
+                                                                       
+                                                                       if ( conv !== true ) {
+                                                                               response = conv ? conv( response ) : conv2( conv1( response ) );
+                                                                       }
                                                                }
                                                        } else if ( s.dataFilter ) {
                                                                
                                                                response = s.dataFilter( response );
                                                                dataTypes = s.dataTypes;
                                                        }
-                                                       
-                                                       prev = current;
                                                }
                
                                                // We have a real success
@@ -504,9 +494,10 @@ jQuery.extend({
                        }
                }
                
-               // Attach deferreds     
-               jXHR.success = jXHR.then = deferred.then;
-               jXHR.error = jXHR.fail = deferred.fail;
+               // Attach deferreds
+               deferred.promise( jXHR );
+               jXHR.success = jXHR.then;
+               jXHR.error = jXHR.fail;
                jXHR.complete = completeDeferred.then;
 
                // Remove hash character (#7531: and string promotion)
@@ -652,19 +643,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.
@@ -697,7 +688,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
@@ -710,7 +701,7 @@ function buildParams( prefix, obj, traditional, add ) {
                                buildParams( prefix + "[" + k + "]", v, traditional, add );
                        });
                }
-                                       
+
        } else {
                // Serialize scalar item.
                add( prefix, obj );
@@ -856,7 +847,7 @@ jQuery.each( [ "prefilter" , "transport" ] , function( _ , name ) {
 // (for those transports that can give text or xml responses)
 function determineDataType( s , ct , text , xml ) {
        
-       var autoDataType = s.autoDataType,
+       var contents = s.contents,
                type,
                regexp,
                dataTypes = s.dataTypes,
@@ -866,8 +857,8 @@ function determineDataType( s , ct , text , xml ) {
        // Auto (xml, json, script or text determined given headers)
        if ( transportDataType === "*" ) {
 
-               for ( type in autoDataType ) {
-                       if ( ( regexp = autoDataType[ type ] ) && regexp.test( ct ) ) {
+               for ( type in contents ) {
+                       if ( ( regexp = contents[ type ] ) && regexp.test( ct ) ) {
                                transportDataType = dataTypes[0] = type;
                                break;
                        }
@@ -886,7 +877,7 @@ function determineDataType( s , ct , text , xml ) {
                
                response = text;
                
-               // If it's not really text, defer to dataConverters
+               // If it's not really text, defer to converters
                if ( transportDataType !== "text" ) {
                        dataTypes.unshift( "text" );
                }
@@ -910,7 +901,7 @@ if ( window.ActiveXObject ) {
                        return new window.XMLHttpRequest();
                } catch( xhrError ) {}
        }
-       
+
        try {
                return new window.ActiveXObject("Microsoft.XMLHTTP");
        } catch( activeError ) {}