Merge branch 'master' of github.com:jquery/jquery
[jquery.git] / src / ajax.js
index 360c87c..71bdd46 100644 (file)
@@ -1,12 +1,16 @@
-var jsc = now(),
-       rscript = /<script(.|\s)*?\/script>/gi,
-       rselectTextarea = /select|textarea/i,
-       rinput = /color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week/i,
+(function( jQuery ) {
+
+var jsc = jQuery.now(),
+       rscript = /<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi,
+       rselectTextarea = /^(?:select|textarea)/i,
+       rinput = /^(?:color|date|datetime|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,
+       rbracket = /\[\]$/,
        jsre = /\=\?(&|$)/,
        rquery = /\?/,
-       rts = /(\?|&)_=.*?(&|$)/,
+       rts = /([?&])_=[^&]*(&?)/,
        rurl = /^(\w+:)?\/\/([^\/?#]+)/,
        r20 = /%20/g,
+       rhash = /#.*$/,
 
        // Keep a copy of the old load method
        _load = jQuery.fn.load;
@@ -59,7 +63,7 @@ jQuery.fn.extend({
                                        // See if a selector was specified
                                        self.html( selector ?
                                                // Create a dummy div to hold the results
-                                               jQuery("<div />")
+                                               jQuery("<div>")
                                                        // inject the contents of the document in, removing the scripts
                                                        // to avoid any 'Permission Denied' errors in IE
                                                        .append(res.responseText.replace(rscript, ""))
@@ -83,6 +87,7 @@ jQuery.fn.extend({
        serialize: function() {
                return jQuery.param(this.serializeArray());
        },
+
        serializeArray: function() {
                return this.map(function() {
                        return this.elements ? jQuery.makeArray(this.elements) : this;
@@ -114,7 +119,6 @@ jQuery.each( "ajaxStart ajaxStop ajaxComplete ajaxError ajaxSuccess ajaxSend".sp
 });
 
 jQuery.extend({
-
        get: function( url, data, callback, type ) {
                // shift arguments if data argument was omited
                if ( jQuery.isFunction( data ) ) {
@@ -198,15 +202,14 @@ jQuery.extend({
                }
        },
 
-       // Last-Modified header cache for next request
-       lastModified: {},
-       etag: {},
-
        ajax: function( origSettings ) {
                var s = jQuery.extend(true, {}, jQuery.ajaxSettings, origSettings),
                        jsonp, status, data, type = s.type.toUpperCase();
 
-               s.context = origSettings && origSettings.context || s;
+               s.url = s.url.replace( rhash, "" );
+
+               // Use original (not extended) context object if it was provided
+               s.context = origSettings && origSettings.context != null ? origSettings.context : s;
 
                // convert data if not already a string
                if ( s.data && s.processData && typeof s.data !== "string" ) {
@@ -241,17 +244,25 @@ jQuery.extend({
                        s.dataType = "script";
 
                        // Handle JSONP-style loading
-                       window[ jsonp ] = window[ jsonp ] || function( tmp ) {
+                       var customJsonp = window[ jsonp ];
+
+                       window[ jsonp ] = function( tmp ) {
                                data = tmp;
                                jQuery.ajax.handleSuccess( s, xhr, status, data );
                                jQuery.ajax.handleComplete( s, xhr, status, data );
-                               // Garbage collect
-                               window[ jsonp ] = undefined;
+                               
+                               if ( jQuery.isFunction( customJsonp ) ) {
+                                       customJsonp( tmp );
 
-                               try {
-                                       delete window[ jsonp ];
-                               } catch( jsonpError ) {}
+                               } else {
+                                       // Garbage collect
+                                       window[ jsonp ] = undefined;
 
+                                       try {
+                                               delete window[ jsonp ];
+                                       } catch( jsonpError ) {}
+                               }
+                               
                                if ( head ) {
                                        head.removeChild( script );
                                }
@@ -263,7 +274,7 @@ jQuery.extend({
                }
 
                if ( s.cache === false && type === "GET" ) {
-                       var ts = now();
+                       var ts = jQuery.now();
 
                        // try replacing _= if it is there
                        var ret = s.url.replace(rts, "$1_=" + ts + "$2");
@@ -291,10 +302,10 @@ jQuery.extend({
                if ( s.dataType === "script" && type === "GET" && remote ) {
                        var head = document.getElementsByTagName("head")[0] || document.documentElement;
                        var script = document.createElement("script");
-                       script.src = s.url;
                        if ( s.scriptCharset ) {
                                script.charset = s.scriptCharset;
                        }
+                       script.src = s.url;
 
                        // Handle Script loading
                        if ( !jsonp ) {
@@ -355,8 +366,8 @@ jQuery.extend({
                                        xhr.setRequestHeader("If-Modified-Since", jQuery.lastModified[s.url]);
                                }
 
-                               if ( jQuery.etag[s.url] ) {
-                                       xhr.setRequestHeader("If-None-Match", jQuery.etag[s.url]);
+                               if ( jQuery.ajax.etag[s.url] ) {
+                                       xhr.setRequestHeader("If-None-Match", jQuery.ajax.etag[s.url]);
                                }
                        }
 
@@ -368,7 +379,7 @@ jQuery.extend({
 
                        // Set the Accepts header for the server, depending on the dataType
                        xhr.setRequestHeader("Accept", s.dataType && s.accepts[ s.dataType ] ?
-                               s.accepts[ s.dataType ] + ", */*" :
+                               s.accepts[ s.dataType ] + ", */*; q=0.01" :
                                s.accepts._default );
                } catch( headerError ) {}
 
@@ -440,7 +451,9 @@ jQuery.extend({
                                }
 
                                // Fire the complete handlers
-                               jQuery.ajax.handleComplete( s, xhr, status, data );
+                               if ( !jsonp ) {
+                                       jQuery.ajax.handleComplete( s, xhr, status, data );
+                               }
 
                                if ( isTimeout === "timeout" ) {
                                        xhr.abort();
@@ -478,7 +491,7 @@ jQuery.extend({
 
                // Send the data
                try {
-                       xhr.send( type === "POST" || type === "PUT" || type === "DELETE" ? s.data : null );
+                       xhr.send( (type !== "GET" && s.data) || null );
 
                } catch( sendError ) {
                        jQuery.ajax.handleError( s, xhr, null, e );
@@ -531,10 +544,10 @@ jQuery.extend({
 });
 
 function buildParams( prefix, obj, traditional, add ) {
-       if ( jQuery.isArray(obj) ) {
+       if ( jQuery.isArray(obj) && obj.length ) {
                // Serialize array item.
                jQuery.each( obj, function( i, v ) {
-                       if ( traditional || /\[\]$/.test( prefix ) ) {
+                       if ( traditional || rbracket.test( prefix ) ) {
                                // Treat each array item as a scalar.
                                add( prefix, v );
 
@@ -551,10 +564,15 @@ function buildParams( prefix, obj, traditional, add ) {
                });
                        
        } else if ( !traditional && obj != null && typeof obj === "object" ) {
+               if ( jQuery.isEmptyObject( obj ) ) {
+                       add( prefix, "" );
+
                // Serialize object item.
-               jQuery.each( obj, function( k, v ) {
-                       buildParams( prefix + "[" + k + "]", v, traditional, add );
-               });
+               } else {
+                       jQuery.each( obj, function( k, v ) {
+                               buildParams( prefix + "[" + k + "]", v, traditional, add );
+                       });
+               }
                                        
        } else {
                // Serialize scalar item.
@@ -567,6 +585,10 @@ jQuery.extend( jQuery.ajax, {
        // Counter for holding the number of active queries
        active: 0,
 
+       // Last-Modified header cache for next request
+       lastModified: {},
+       etag: {},
+
        handleError: function( s, xhr, status, e ) {
                // If a local callback was specified, fire it
                if ( s.error ) {
@@ -617,9 +639,8 @@ jQuery.extend( jQuery.ajax, {
                try {
                        // IE error sometimes returns 1223 when it should be 204 so treat it as success, see #1450
                        return !xhr.status && location.protocol === "file:" ||
-                               // Opera returns 0 when status is 304
-                               ( xhr.status >= 200 && xhr.status < 300 ) ||
-                               xhr.status === 304 || xhr.status === 1223 || xhr.status === 0;
+                               xhr.status >= 200 && xhr.status < 300 ||
+                               xhr.status === 304 || xhr.status === 1223;
                } catch(e) {}
 
                return false;
@@ -631,15 +652,14 @@ jQuery.extend( jQuery.ajax, {
                        etag = xhr.getResponseHeader("Etag");
 
                if ( lastModified ) {
-                       jQuery.lastModified[url] = lastModified;
+                       jQuery.ajax.lastModified[url] = lastModified;
                }
 
                if ( etag ) {
-                       jQuery.etag[url] = etag;
+                       jQuery.ajax.etag[url] = etag;
                }
 
-               // Opera returns 0 when status is 304
-               return xhr.status === 304 || xhr.status === 0;
+               return xhr.status === 304;
        },
 
        httpData: function( xhr, type, s ) {
@@ -673,3 +693,11 @@ jQuery.extend( jQuery.ajax, {
        }
 
 });
+
+// Does this browser support XHR requests?
+jQuery.support.ajax = !!jQuery.ajaxSettings.xhr();
+
+// For backwards compatibility
+jQuery.extend( jQuery.ajax );
+
+})( jQuery );