Fixed #471
[jquery.git] / src / ajax / ajax.js
index d56ae0f..fa48afa 100644 (file)
@@ -134,10 +134,7 @@ jQuery.fn.extend({
 // If IE is used, create a wrapper for the XMLHttpRequest object
 if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
        XMLHttpRequest = function(){
-               return new ActiveXObject(
-                       navigator.userAgent.indexOf("MSIE 5") >= 0 ?
-                       "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP"
-               );
+               return new ActiveXObject("Microsoft.XMLHTTP");
        };
 
 // Attach a bunch of functions for handling common AJAX events
@@ -173,7 +170,10 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
 /**
  * Attach a function to be executed whenever an AJAX request completes.
  *
- * @example $("#msg").ajaxComplete(function(){
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxComplete(function(request, settings){
  *   $(this).append("<li>Request Complete.</li>");
  * });
  * @desc Show a message when an AJAX request completes.
@@ -188,7 +188,10 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
  * Attach a function to be executed whenever an AJAX request completes
  * successfully.
  *
- * @example $("#msg").ajaxSuccess(function(){
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxSuccess(function(request, settings){
  *   $(this).append("<li>Successful Request!</li>");
  * });
  * @desc Show a message when an AJAX request completes successfully.
@@ -202,8 +205,11 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
 /**
  * Attach a function to be executed whenever an AJAX request fails.
  *
- * @example $("#msg").ajaxError(function(){
- *   $(this).append("<li>Error requesting page.</li>");
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxError(function(request, settings){
+ *   $(this).append("<li>Error requesting page " + settings.url + "</li>");
  * });
  * @desc Show a message when an AJAX request fails.
  *
@@ -212,9 +218,26 @@ if ( jQuery.browser.msie && typeof XMLHttpRequest == "undefined" )
  * @param Function callback The function to execute.
  * @cat AJAX
  */
+/**
+ * Attach a function to be executed before an AJAX request is send.
+ *
+ * The XMLHttpRequest and settings used for that request are passed
+ * as arguments to the callback.
+ *
+ * @example $("#msg").ajaxSend(function(request, settings){
+ *   $(this).append("<li>Starting request at " + settings.url + "</li>");
+ * });
+ * @desc Show a message before an AJAX request is send.
+ *
+ * @name ajaxSend
+ * @type jQuery
+ * @param Function callback The function to execute.
+ * @cat AJAX
+ */
 
 new function(){
-       var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess".split(",");
+       var e = "ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(",");
 
        for ( var i = 0; i < e.length; i++ ) new function(){
                var o = e[i];
@@ -487,6 +510,9 @@ jQuery.extend({
         * (Boolean) async - By default, all requests are send asynchronous (set to true).
         * If you need synchronous requests, set this option to false.
         *
+        * (Function) beforeSend - A pre-callback to set custom headers etc., the
+        * XMLHttpRequest is passed as the only argument.
+        *
         * @example $.ajax({
         *   type: "GET",
         *   url: "test.js",
@@ -524,7 +550,8 @@ jQuery.extend({
                        data: null,
                        contentType: "application/x-www-form-urlencoded",
                        processData: true,
-                       async: true
+                       async: true,
+                       beforeSend: null
                }, s);
 
                // if data available
@@ -565,6 +592,12 @@ jQuery.extend({
                // Make sure the browser sends the right content length
                if ( xml.overrideMimeType )
                        xml.setRequestHeader("Connection", "close");
+                       
+               // Allow custom headers/mimetypes
+               if( s.beforeSend )
+                       s.beforeSend(xml);
+               if (s.global)
+                   jQuery.event.trigger("ajaxSend", [xml, s]);
 
                // Wait for a response to come back
                var onreadystatechange = function(isTimeout){
@@ -595,7 +628,7 @@ jQuery.extend({
 
                                        // Fire the global callback
                                        if( s.global )
-                                               jQuery.event.trigger( "ajaxSuccess" );
+                                               jQuery.event.trigger( "ajaxSuccess", [xml, s] );
 
                                // Otherwise, the request was not successful
                                } else {
@@ -604,12 +637,12 @@ jQuery.extend({
 
                                        // Fire the global callback
                                        if( s.global )
-                                               jQuery.event.trigger( "ajaxError" );
+                                               jQuery.event.trigger( "ajaxError", [xml, s] );
                                }
 
                                // The request was completed
                                if( s.global )
-                                       jQuery.event.trigger( "ajaxComplete" );
+                                       jQuery.event.trigger( "ajaxComplete", [xml, s] );
 
                                // Handle the global AJAX counter
                                if ( s.global && ! --jQuery.active )