Javascript 从另一页返回时,从page onload事件调用xmlhttprequest无法正常工作

Javascript 从另一页返回时,从page onload事件调用xmlhttprequest无法正常工作,javascript,xmlhttprequest,onload,Javascript,Xmlhttprequest,Onload,通过按链接从我的页面转到另一个页面,然后返回到当前页面,然后从onload事件调用xmlhttprequest,xhr.open和xhr.send的js代码正在工作,但是xhr调用的服务器端代码没有运行,并且在xhr.onreadystatechange函数中,xhr.responseText正在返回他的旧值 在这种情况下,从另一个页面返回时,从document.onreadystatechange事件调用xmlhttprequest也无法正常工作 当我在第一个页面加载中从onload调用它时(

通过按链接从我的页面转到另一个页面,然后返回到当前页面,然后从onload事件调用xmlhttprequest,xhr.open和xhr.send的js代码正在工作,但是xhr调用的服务器端代码没有运行,并且在xhr.onreadystatechange函数中,xhr.responseText正在返回他的旧值

在这种情况下,从另一个页面返回时,从document.onreadystatechange事件调用xmlhttprequest也无法正常工作

当我在第一个页面加载中从onload调用它时(在转到另一个页面并返回之前),或者当我通过单击按钮调用它时,相同的代码可以正常工作

从另一个页面返回时,为什么从onload事件调用xmlhttprequest无法正常工作

我用的是Chrome

这是我的密码:

<script type="text/javascript">
function CallAJAX() {
    xhr = null;
    if (window.XMLHttpRequest) {
        xhr = new XMLHttpRequest();
    }
    else {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhr.onreadystatechange = AJAX_onreadystatechange;
    xhr.open("GET", '/MyPage.aspx', true);
    xhr.send(null);
}
function AJAX_onreadystatechange() {
    if (xhr.readyState == 4 && xhr.status == 200) {
        alert('xhr.responseText=' + xhr.responseText);
    }
}
window.onload = function () {
    //alert is working also after going to link and than back to current page
    alert('alert')
    //Call to xmlhttprequest from page onload event - is Not working OK - after going to link and than back to current page
    CallAJAX();
}
</script>
<!--here is the "link" mentioned in the above comment-->
<a href="http://www.google.com">link</a>
<!--Call to xmlhttprequest from button onclick event - is working OK-->
<input id="Button1" type="button" value="button" onclick="CallAJAX();" />

您应该发布您的代码…尝试使用:
setTimeout(functionwhothausehmlhttprequest(),0)
在GChrome中,打开开发工具(F12),切换到
Network
选项卡,单击“过滤器”图标->选择
XHR
。然后,重新加载页面并检查您的请求,查看客户端发送给服务器的内容以及服务器对客户端的响应。我在上面添加了代码,使问题更加准确。thanx Pascal Le Merrer。我在上面的帖子中添加了更新答案,这是我从Reegan那里得到的消息。谢谢你们。
    xhr.open("GET", '/MyPage.aspx?uniqueParamVal=' + new Date().getTime(), true);