- jquery.ajax是基于XMLHttpRequest,所以直接写能绕过jquery不同版本的问题。
- XMLHttpRequest.progress和onprogress都不好用,最后是用监听XMLHttpRequest.upload.addEventListener("progress", function () { 方法}),而且要在open之前
var xhr = new XMLHttpRequest();
try { //加载中 Showloading(); } catch (err) { } xhr.open('POST', url); xhr.setRequestHeader("Content-type", "multipart/form-data"); xhr.responseType = 'json'; xhr.send(formData); xhr.onload = function (res) { console.log('done', res.target.response) $("#loadDIV").remove(); var result = res.target.response if (cusobj) { result.cusobj = cusobj } if (typeof (options.success) == 'function') { options.success(result); } } xhr.onerror = function (res) { alert("上传失败"); } xhr.onloadend = function (res) { $("#loadDIV").remove(); }
function Showloading() { var div1 = '<div id="loadDIV" class="layerbg"><div class="waitlayer"><div class="wtimg"><img src="/images/loading1.gif" width="32" height="32" alt=""/></div>'; div1 += '<div class="wttxt">加载中...</div></div></div>'; $(document.body).append(div1); }