ajax参数xhrfields,使用xhrFields可以将进度功能添加到jQuery.ajax()中吗?

博客内容介绍了如何在jQuery的Ajax调用中设置和使用XMLHttpRequest对象的上传进度事件,特别是针对POST请求时的数据发送进度。通过xhr.upload.onprogress事件监听上传进度,并在xhr.upload.onload事件中处理完成状态。这有助于在文件上传过程中提供进度反馈。
摘要由CSDN通过智能技术生成

简短的回答:

不,你不能这样做,你想用什么xhrFields。

龙答:

有一个XmlHttpRequest对象两份进度事件:

响应研究的进展(XmlHttpRequest.onprogress)

这是当浏览器下载从数据服务器。

请求进度(XmlHttpRequest.upload.onprogress)

这是当浏览器发送数据到服务器(包括POST参数,Cookie和文件)

在你的代码使用响应进度事件,但您需要的是请求进度事件。这是你如何做到这一点:

$.ajax({

async: true,

contentType: file.type,

data: file,

dataType: 'xml',

processData: false,

success: function(xml){

// Do stuff with the returned xml

},

type: 'post',

url: '/fileuploader/' + file.name,

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选项参数必须是返回jQuery来使用原生XMLHttpRequest对象的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值