【问题标题】:body onLoad XMLHttpRequest正文 onLoad XMLHttpRequest
【发布时间】:2016-11-25 09:44:00
【问题描述】:

html 文档正文 onLoad 函数可以包含 XMLHttpRequest 吗? onLoad 函数运行并且正在发送 XMLHttpRequest。 从那里看来,它似乎被忽略了; onreadystatechange 函数是 不叫。 随后的 XMLHttpRequests 被正确处理。所以我想知道 关于它在 onLoad 函数中的使用 使用 Firefox 47.0 运行。

var ifm = "If-Modified-Since";
var ifmDate = "Sat, 01 Jan 2000 00:00:00 GMT";

function initPage() {
  document.getElementById("refreshratetxt").value = interval;
  refreshPage();
  timerval = setInterval("refreshPage()", interval * 1000);
}

function refreshPage() {
  sensordataGet();
}

function createXHR() {
  if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    return new XMLHttpRequest();
  }
<code removed here>
}

 function sensordataGet() {
  var sensordata = createXHR();
  sensordata.open("GET", "sensordataget.py", true); // _sensordataget
  sensordata.setRequestHeader(ifm, ifmDate);
  sensordata.onreadystatechange = function () {
    if (sensordata.readyState == 4) {
      if (sensordata.status == 200) {
        var response = JSON.parse(sensordata.responseText);
        <code removed here>
      }
    }
  }
  sensordata.send();
}

<body onLoad="initPage()">

【问题讨论】:

    标签: javascript firefox xmlhttprequest onload


    【解决方案1】:

    你可以参考这个...

    <html>  
    <head>  
    <script>  
    var request;  
    function sendInfo()  
    {  
    var v=document.vinform.t1.value;  
    var url="index.jsp?val="+v;  
    
    if(window.XMLHttpRequest){  
    request=new XMLHttpRequest();  
    }  
    else if(window.ActiveXObject){  
    request=new ActiveXObject("Microsoft.XMLHTTP");  
    }  
    
    try  
    {  
    request.onreadystatechange=getInfo;  
    request.open("GET",url,true);  
    request.send();  
    }  
    catch(e)  
    {  
    alert("Unable to connect to server");  
    }  
    }  
    
    function getInfo(){  
    if(request.readyState==4){  
    var val=request.responseText;  
    document.getElementById('amit').innerHTML=val;  
    }  
    }  
    
    </script>  
    </head>  
    <body>  
        <marquee><h1>This is an example of ajax</h1></marquee>  
    <form name="vinform">  
    <input type="text" name="t1">  
    <input type="button" value="ShowTable" onClick="sendInfo()">  
    </form>  
    
    <span id="amit"> </span>  
    
    </body>  
    </html>  
    

    索引.jsp

    <%  
    int n=Integer.parseInt(request.getParameter("val"));  
    
    for(int i=1;i<=10;i++)  
    out.print(i*n+"<br>");  
    
    %>  
    

    【讨论】:

    • 这是一个例子,但它根本没有使用 body onLoad。
    【解决方案2】:

    简短的回答是,你可以在 onLoad 函数中有一个 XMLHttpRequest。长答案是我看到的问题是由 sensordataget python 函数中的计时问题引起的。

    【讨论】:

      【解决方案3】:

      此代码应该可以完成您想要的工作:

          <script>
             window.onload = yourFunction(){
                 /* executed when page is loaded*/
             };
          </script>
      

      【讨论】:

        猜你喜欢
        • 2017-10-12
        • 2023-03-17
        • 1970-01-01
        • 1970-01-01
        • 2016-02-09
        • 2012-02-29
        • 2022-01-20
        • 1970-01-01
        • 2019-12-06
        相关资源
        最近更新 更多