diff --git a/README.md b/README.md index 151b2ba..dc6d5bd 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,7 @@ Parameters: + string (sets content type to 'application/x-www-form-urlencoded' if not set in headers) + FormData (doesn't set content type so that browser will set as appropriate) - `method`: 'GET', 'POST', etc. Defaults to 'GET' or 'POST' based on body +- `onprogress`: function callback, will be called multiple times with a progress event when uploading files using FormData - `cors`: If your using cross-origin, you will need this true for IE8-9 (to use the XDomainRequest object, also see [Compatibility](#compatibility)) The following parameters are passed directly onto the request object: diff --git a/index.js b/index.js index b9bdc22..f2759dc 100644 --- a/index.js +++ b/index.js @@ -81,8 +81,13 @@ exports.ajax = function (params, callback) { for (var i = 0, len = reqfields.length, field; i < len; i++) { field = reqfields[i] - if (params[field] !== undefined) - req[field] = params[field] + if (params[field] !== undefined){ + if(body && global.FormData && body instanceof global.FormData && field == 'onprogress'){ + req.upload[field] = params[field]; + }else{ + req[field] = params[field]; + } + } } for (var field in headers) diff --git a/nanoajax.min.js b/nanoajax.min.js index bbe0310..4a8211b 100644 --- a/nanoajax.min.js +++ b/nanoajax.min.js @@ -1,2 +1,2 @@ // https://github.com/yanatan16/nanoajax -!function(t,e){function n(t){return t&&e.XDomainRequest&&!/MSIE 1/.test(navigator.userAgent)?new XDomainRequest:e.XMLHttpRequest?new XMLHttpRequest:void 0}function o(t,e,n){t[e]=t[e]||n}var r=["responseType","withCredentials","timeout","onprogress"];t.ajax=function(t,a){function s(t,e){return function(){c||(a(void 0===f.status?t:f.status,0===f.status?"Error":f.response||f.responseText||e,f),c=!0)}}var u=t.headers||{},i=t.body,d=t.method||(i?"POST":"GET"),c=!1,f=n(t.cors);f.open(d,t.url,!0);var l=f.onload=s(200);f.onreadystatechange=function(){4===f.readyState&&l()},f.onerror=s(null,"Error"),f.ontimeout=s(null,"Timeout"),f.onabort=s(null,"Abort"),i&&(o(u,"X-Requested-With","XMLHttpRequest"),e.FormData&&i instanceof e.FormData||o(u,"Content-Type","application/x-www-form-urlencoded"));for(var p,m=0,v=r.length;v>m;m++)p=r[m],void 0!==t[p]&&(f[p]=t[p]);for(var p in u)f.setRequestHeader(p,u[p]);return f.send(i),f},e.nanoajax=t}({},function(){return this}()); \ No newline at end of file +!function(t,e){function n(t){return t&&e.XDomainRequest&&!/MSIE 1/.test(navigator.userAgent)?new XDomainRequest:e.XMLHttpRequest?new XMLHttpRequest:void 0}function o(t,e,n){t[e]=t[e]||n}e.nanoajax=t;var r=["responseType","withCredentials","timeout","onprogress"];t.ajax=function(t,a){function s(t,e){return function(){c||(a(void 0===p.status?t:p.status,0===p.status?"Error":p.response||p.responseText||e,p),c=!0)}}var u=t.headers||{},i=t.body,d=t.method||(i?"POST":"GET"),c=!1,p=n(t.cors);p.open(d,t.url,!0);var f=p.onload=s(200);p.onreadystatechange=function(){4===p.readyState&&f()},p.onerror=s(null,"Error"),p.ontimeout=s(null,"Timeout"),p.onabort=s(null,"Abort"),i&&(o(u,"X-Requested-With","XMLHttpRequest"),e.FormData&&i instanceof e.FormData||o(u,"Content-Type","application/x-www-form-urlencoded"));for(var l,m=0,v=r.length;v>m;m++)l=r[m],void 0!==t[l]&&(i&&e.FormData&&i instanceof e.FormData&&"onprogress"==l?p.upload[l]=t[l]:p[l]=t[l]);for(var l in u)p.setRequestHeader(l,u[l]);return p.send(i),p}}({},function(){return this}()); diff --git a/test/index.js b/test/index.js index 2605300..6d0c0f9 100644 --- a/test/index.js +++ b/test/index.js @@ -100,6 +100,28 @@ function defineTests(ajax) { done() }) }) + + test('FormData progress', function(done){ + var formData = new FormData(); + formData.append("arg", "value"); + formData.append("foo", "bar"); + + var isLengthComputable = false; + + var progressHandler = function(e){ + isLengthComputable = e.lengthComputable; + } + + ajax({url: '/post', + body: formData, + method: 'POST', + onprogress: progressHandler + }, function (code, body) { + assert.equal(isLengthComputable, true) + done() + }) + + }) } // Safari: