Perché il mio onbeforeunload non funziona ora mentre lo era prima?Oppure AJAX fallisce al secondo comando identico?

StackOverflow https://stackoverflow.com/questions/564676

  •  05-09-2019
  •  | 
  •  

Domanda

Ciò segue da Questo domanda

Questo era lavorando:

<body onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=266&UserID=11631');"> 

Questo è stato creato utilizzando quanto segue nella pagina aspx:

<body onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=<%= Session["QueryId"] %>&
 UserID=<%= Session["UserID"] %>')">

Questo è non lavorando:

<body id="uxBodyTag" onbeforeunload=
 "ajaxRequest('UnlockQuery.ashx?QueryID=266&amp;UserID=11631');"> 

Questo viene creato utilizzando:

uxBodyTag.Attributes["onbeforeunload"] += 
 "ajaxRequest('UnlockQuery.ashx?QueryID=" + 
 queryId.ToString() + "&UserID=" + Session["UserID"].ToString() + "');";

Il codice richiamato è questo:

function ajaxRequest(url)
{
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {   // code for all new browsers
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {   // code for IE5 and IE6
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
        xmlhttp.onreadystatechange=null;
        xmlhttp.open("GET",url,true);
        xmlhttp.send(null);
    }
}

MODIFICARE:

Sembra fallire solo quando vengono effettuate chiamate successive allo stesso sblocco.Penso che questo potrebbe essere un problema AJAX....

È stato utile?

Soluzione

Aggiunta

&date=DateTime.now.Ticks.ToString()

sembra averlo risolto.Non penso che a IE7 piaccia quando arriva la stessa chiamata AJAX e la precedente non è stata "risolta" (la pagina viene eliminata prima che ritorni la chiamata AJAX).

Grazie a tutti coloro che hanno fornito aiuto.

Altri suggerimenti

Per i mezzi di debug, abbiamo provato:

alert(url);
xmlhttp.open("GET",url,true);

che ha dato il risultato atteso di:

UnlockQuery.ashx?QueryID=319&UserID=11648

Ora possiamo controllare ciò che il server ha da dire attraverso:

xmlhttp.onreadystatechange = function() { 
  if (this.readyState == 4) alert(this.status + ": " + this.StatusText); 
};

EDIT:

Come si è visto, la cache del browser è stato il motivo per i risultati inaspettati. Suggerisco di vietare la memorizzazione nella cache della pagina AJAX attraverso opportuni intestazioni HTTP (Pragma, Cache-Control, Expires).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top