如何将进度事件侦听器添加到原型 Ajax 请求?
How to add a progress event listener to a prototype Ajax request?
如何向原型 Ajax 请求添加进度事件侦听器?
我在原型文档中没有找到关于此的任何内容..
我找到了一些使用 jQuery 但没有使用原型的示例。
new Ajax.Request('/some_url', {
method:'get',
onSuccess: function(transport) {..},
onFailure: function() {..}
});
使用 jQuery:
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
// Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
// Download progress
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
// Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){..}
});
这是我在代码中用于执行 AJAX 文件上传的示例
new Ajax.Request('backend.php',{'method':'get','onCreate':function(t){
t.transport.upload.onprogress = function(event){
if(event.lengthComputable)
{
console.log((event.loaded / event.total * 100 | 0)+'%');
}
}
},'onSuccess':function(result){
console.log("success")
}});
如何向原型 Ajax 请求添加进度事件侦听器?
我在原型文档中没有找到关于此的任何内容..
我找到了一些使用 jQuery 但没有使用原型的示例。
new Ajax.Request('/some_url', {
method:'get',
onSuccess: function(transport) {..},
onFailure: function() {..}
});
使用 jQuery:
$.ajax({
xhr: function() {
var xhr = new window.XMLHttpRequest();
// Upload progress
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
//Do something with upload progress
console.log(percentComplete);
}
}, false);
// Download progress
xhr.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
// Do something with download progress
console.log(percentComplete);
}
}, false);
return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){..}
});
这是我在代码中用于执行 AJAX 文件上传的示例
new Ajax.Request('backend.php',{'method':'get','onCreate':function(t){
t.transport.upload.onprogress = function(event){
if(event.lengthComputable)
{
console.log((event.loaded / event.total * 100 | 0)+'%');
}
}
},'onSuccess':function(result){
console.log("success")
}});