[英]Send XHR request without receiving any data
我正在嘗試制作一個非常簡單的腳本,該腳本應該使我保持登錄網站的狀態,該腳本會在閑置10分鍾后刪除您的會話。 這非常簡單,如下所示:
//Silently "refresh" the page - at least server thinks you refreshed, thus you're active
function call() {
var req = new XMLHttpRequest();
//Load current url
req.open("GET",location.href,true);
//Try to only send the data! (does not work - the browser also receives data)
req.onprogress = function() {
this.abort(); //Abort request
wait(); //Wait another 5 minutes
}
//Repeat request instantly if it fails
req.onerror = call;
//Send
req.send();
}
function wait() {
//5minutes timeout
setTimeout(call,5000);
}
wait();
這可以正常工作,但請求似乎已完全加載。 盡管頁面很小,但我想使它整潔並防止下載數據。 這意味着,我要在數據開始下載后立即停止加載。 或者更好-在數據發送之后。
有沒有辦法使這種“ ping”功能?
我嘗試了這段代碼:
var req = new XMLHttpRequest();
req.onreadystatechange = function(){
console.log( this.readyState );
if( this.readyState == 2 ) { //sent, otherwise it raises an error
console.log( 'aborting...' );
this.abort()
}
if( this.readyState == 4 ) {
console.log( this.responseText );
}
}
req.open( 'get', .... );
req.send();
印刷品:
1
2
aborting...
3
4
undefined
我對此不太確定,但是我想通過中止請求會中止數據的下載和檢索,但是會觸發所有其他狀態。 我嘗試使用未緩存的大圖像進行嘗試,並且請求很快完成,沒有任何結果。
順便說一句。 要將“ ping”發送到您的服務器,您還可以將圖像標簽的src
設置為所需的腳本,這也會觸發請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.