1 // jQuery Ajax Native Plugin
3 // (c) 2015 Tarik Zakaria Benmerar, Acigna Inc.
4 // jQuery Ajax Native Plugin may be freely distributed under the MIT license.
5 (function (root, factory) {
6 if (typeof define === 'function' && define.amd) {
7 // AMD. Register as an anonymous module.
8 define(['jquery'], factory);
9 } else if (typeof exports === 'object') {
10 // Node. Does not work with strict CommonJS, but
11 // only CommonJS-like environments that support module.exports,
13 module.exports = factory(require('jquery'));
15 // Browser globals (root is window)
18 }(this, function ( $ ) {
19 var ajaxSettings = $.ajaxSettings;
20 ajaxSettings.responseFields.native = 'responseNative';
21 ajaxSettings.converters[ '* native' ] = true;
25 // file protocol always yields status code 0, assume 200
28 // #1450: sometimes IE returns 1223 when it should be 204
32 xhrSupported = jQuery.ajaxSettings.xhr();
34 // Open requests must be manually aborted on unload (#5280)
35 if ( window.ActiveXObject ) {
36 $( window ).on( "unload", function() {
37 for ( var key in xhrCallbacks ) {
38 xhrCallbacks[ key ]();
42 support.cors = !!xhrSupported && ( "withCredentials" in xhrSupported );
43 support.ajax = xhrSupported = !!xhrSupported;
45 //Native Data Type Ajax Transport
46 $.ajaxTransport('native', function ( options ) {
48 // Cross domain only allowed if supported through XMLHttpRequest
49 if ( support.cors || xhrSupported && !options.crossDomain ) {
51 send: function( headers, complete ) {
57 xhr.open( options.type, options.url, options.async, options.username, options.password );
59 // Apply custom fields if provided
60 if ( options.xhrFields ) {
61 for ( i in options.xhrFields ) {
62 xhr[ i ] = options.xhrFields[ i ];
66 // Override mime type if needed
67 if ( options.mimeType && xhr.overrideMimeType ) {
68 xhr.overrideMimeType( options.mimeType );
71 // X-Requested-With header
72 // For cross-domain requests, seeing as conditions for a preflight are
73 // akin to a jigsaw puzzle, we simply never set it to be sure.
74 // (it can always be set on a per-request basis or even using ajaxSetup)
75 // For same-domain requests, won't change header if already provided.
76 if ( !options.crossDomain && !headers["X-Requested-With"] ) {
77 headers["X-Requested-With"] = "XMLHttpRequest";
81 for ( i in headers ) {
82 xhr.setRequestHeader( i, headers[ i ] );
86 callback = function( type ) {
89 delete xhrCallbacks[ id ];
90 callback = xhr.onload = xhr.onerror = null;
92 if ( type === "abort" ) {
94 } else if ( type === "error" ) {
96 // file: protocol always yields status 0; see #8605, #14207
101 // The native response associated with the responseType
102 // Stored in the xhr.response attribute (XHR2 Spec)
103 if ( xhr.response ) {
104 responses.native = xhr.response;
108 xhrSuccessStatus[ xhr.status ] || xhr.status,
111 xhr.getAllResponseHeaders()
119 xhr.onload = callback();
120 xhr.onerror = callback("error");
122 // Create the abort callback
123 callback = xhrCallbacks[ id ] = callback("abort");
126 // Do send the request (this may raise an exception)
127 xhr.send( options.hasContent && options.data || null );
129 // #14683: Only rethrow if this hasn't been notified as an error yet
146 //$.getNative wrapper
147 $.getNative = function ( url, callback ) {
152 responseType: 'arraybuffer'
159 $.getBlob = function ( url, callback ) {
170 //Return the jQuery Object