//////////////////////////////////////// ////////////////////////// // Options is the request option // // The originalOptions value is used as the option provided to the Ajax method without modification. Therefore, the default value in the ajaxSettings setting does not exist // // JqXHR is the requested jqXHR object // //////////////////////////////////////// ////////////////////////// $. AjaxPrefilter ("image", function (options, originalOptions, jqXHR ){ // Convert the Data Type through the pre-processor If (options. test ){ Options. type = 'get' } // Add a prefix Options. url = "http://img.mukewang.com/" + options. url }); /////////////////////// // Request distributor transports // /////////////////////// $. AjaxTransport ("image", function (s ){ If (s. type = "GET" & s. async ){ Var image; Return { Send: function (_, callback ){ Image = new Image (); Function done (status ){ If (image ){ Var statusText = (status = 200 )? "Success": "error ", Tmp = image; Image = image. onreadystatechange = image. onerror = image. onload = null; Callback (status, statusText ,{ Image: tmp }); } } Image. onreadystatechange = image. onload = function (){ Done (200 ); }; Image. onerror = function (){ Done (404 ); }; Show (s. url) Image. src = s. url; }, Abort: function (){ If (image ){ Image = image. onreadystatechange = image. onerror = image. onload = null; } } }; } }); $ ("# Test"). click (function (){ // Execute an asynchronous HTTP (Ajax) request. Var ajax = $. ajax ({ Test: true, // test Url: '547d5a45000156f406000338-590-330.jpg ', DataType: 'image ', Type: 'post ', Data :{ Foo: ["bar1", "bar2"] }, // This object is used to set the context of Ajax-related callback Functions Context: document. body, // The callback function before the request is sent to modify jqXHR before the request is sent BeforeSend: function (xhr ){ Xhr. overrideMimeType ("text/plain; charset = x-user-defined "); Show ('partial event beforesend ') }, // Callback function after the request is complete (called after success and error) Complete: function (){ Show ('partial event complete ') }, Error: function (){ Show ('call this function when a local event error request fails ') }, Success: function (){ Show ('local event success ') } }) Ajax. done (function (){ Show ('done ') }). Fail (function (){ Show ('fail ') }). Always (function (){ Show ('alway ') }) |