2017-08-11 105 views
0

如何將進度事件偵聽器添加到原型Ajax請求中?如何將進度事件偵聽器添加到原型Ajax請求中?

我沒有找到有關此內容的原型DOC什麼..

我發現使用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){..} 
}); 

回答

1

下面是我在我的代碼使用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") 
}}); 
+0

感謝一個例子!我還加了't.transport.onprogress = function(event){..}' – vhanahrni

相關問題