简答:
不,你不能使用xhrFields做你想要的。
长答案:
XmlHttpRequest对象中有两个进度事件:
>响应进度(XmlHttpRequest.onprogress)
这是当浏览器从服务器下载数据时。
>请求进度(XmlHttpRequest.upload.onprogress)
这是当浏览器将数据发送到服务器时(包括POST参数,Cookie和文件)
在您的代码中,您正在使用响应进度事件,但您需要的是请求进度事件。这是你如何做的:
$.ajax({
async: true,success: function(xml){
// Do stuff with the returned xml
},xhr: function(){
// get the native XmlHttpRequest object
var xhr = $.ajaxSettings.xhr() ;
// set the onprogress event handler
xhr.upload.onprogress = function(evt){ console.log('progress',evt.loaded/evt.total*100) } ;
// set the onload event handler
xhr.upload.onload = function(){ console.log('DONE!') } ;
// return the customized object
return xhr ;
}
});
xhr选项参数必须是返回一个本机XmlHttpRequest对象以供jQuery使用的函数。