How do I submit get request from javascript to server without using a form

问题内容:

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  
Well good luck doing it without JavaScript. It would be a lot more efficient than reloading the page over and over again. Why remove the form if it is working? Only other choice is to just reload the page with JavaScript or meta tags….
1  
“I just want the javascript to make request to server” That’s exactly what ajax is
    
Google the concept of AJAX
    
your current solution is already one with minimal javascript required …
    
Writing Ajax in plain JS? Hell no! Don’t. Use already written ajax lib or use another lib’s that already have those and more.

答案:

答案1:

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.

答案评论:

    
thanks , actually reload doesnt work(), firstly for some reason it is jerky whereas for submit is not, secondly a detail I missed is that when the form checks if it finds the task has completed the server then redirects to a different results page, of course with reload the redirect doesnt work. So is there way I can simply hide the form, I may come back to the ajax way at a later date if I need to.
– Paul Taylor
52 secs ago

原文地址:

https://stackoverflow.com/questions/47756006/how-do-i-submit-get-request-from-javascript-to-server-without-using-a-form

Add a Comment