dsyua2828
dsyua2828
2017-12-11 15:19
浏览 65

如何在不使用表单的情况下将JavaScript请求从服务器提交到服务器

This a local webserver app

I have a form representing progress with a single submit button, this submits to my server and then the server will construct a new page with updated progress bars, so the users see the progress bars updated every time they click on the 'Check Progress' button.

But what I wanted it to do is automatically check every 5 seconds without needing the user to press button. So I have a javasscript that submits the form after 5 seconds, so every 5 seconds the page is replaced with a new page with updated progress bars.

This works great, so now I dont need the form button. But If i remove the form then my javascript wont work because I no longer have a form to submit.

I read I can use Ajax to talk to server, receive updated data and update existing page. But I dont want to do this i want to keep javascript to a minimum as I find it very difficult to get it working.

I just want the javascript to make request to server that will then replace existing page with new updated page, but how do I make a simple getrequest to server using javascript.

This is what I currently have

....

<tr>
    <td>
        <label for="pb12">
            Errors
        </label>
    </td>
    <td>
        <progress id="pb12" value="0" max="100">
        </progress>
    </td>
</tr>
</table>
<form action="/check_progress" id="check_progress">
    <p>
        <input type="submit" value="Check Progress">
    </p>
</form>
<script>function checkprogress() { document.getElementById('check_progress').submit(); }; setTimeout(checkprogress, 5000)</script></body>
</html>

图片转代码服务由CSDN问答提供 功能建议

这是一个本地网络服务器应用程序

我有一个表示进度的表单 单个提交按钮,这将提交到我的服务器,然后服务器将构建一个包含更新进度条的新页面,因此用户每次单击“检查进度”按钮时都会看到更新的进度条。 \ n

但我想要它做的是每5秒自动检查一次,而无需用户按下按钮。 所以我有一个javasscript在5秒后提交表单,所以每5秒页面被一个带有更新进度条的新页面替换。

这很好用,所以现在我不需要 表单按钮。 但是,如果我删除表单,那么我的javascript将无法工作,因为我不再有提交表单。

我读过我可以使用Ajax与服务器通信,接收更新数据并更新现有页面 。 但是我不想这样做我想将javascript保持在最低限度,因为我觉得很难让它工作。

我只是想让javascript向服务器发出请求然后 用新的更新页面替换现有页面,但是如何使用javascript对服务器进行简单的getrequest。

这是我目前拥有的

。 ...

 &lt; tr&gt; 
&lt; td&gt; 
&lt; label for =“pb12”&gt; 
错误
&lt; / label&gt; \  n&lt; / td&gt; 
&lt; td&gt; 
&lt; progress id =“pb12”value =“0”max =“100”&gt; 
&lt; / progress&gt; 
&lt; / td&gt; 
&lt;  ; / tr&gt; 
&lt; / table&gt; 
&lt; form action =“/ check_progress”id =“check_progress”&gt; 
&lt; p&gt; 
&lt; input type =“submit”value =“Check Progress”&gt  ; 
&lt; / p&gt; 
&lt; / form&gt; 
&lt; script&gt; function checkprogress(){document.getElementById('check_progress')。submit();  };  setTimeout(checkprogress,5000)&lt; / script&gt;&lt; / body&gt; 
&lt; / html&gt; 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxie2613
    dongxie2613 2017-12-11 15:26
    已采纳

    So how can you reload the page without a form? Meta refresh tag

    <meta http-equiv="refresh" content="5">
    

    Just make sure you have the proper no cache headers.

    Or use JavaScript to reload the page

    window.location.reload(true);
    

    Or just submit the form

    document.getElementById("yourForm").submit()
    

    or change the backend to return just the updated content and spit that out with an Ajax call.

    点赞 评论

相关推荐