dsyua2828 2017-12-11 15:19
浏览 106
已采纳

如何在不使用表单的情况下将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>
  • 写回答

1条回答 默认 最新

  • 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示