Let's say I have a web interface to ping: it accepts a hostname/ip and packet count, fires off a shell to ping with the given parameters, and returns the raw output from ping, flushing the output buffer after every newline. So I visit:
https://example.com/ping.php?target=127.0.0.1&num=10
and the browser displays the resulting lines incrementally as they're returned from ping.
Is it possible to put those results, line by line as they're made available, into an arbitrary <pre>
or <div>
element?
My first try only populates the #results
element after the server's response is fully received:
$.get("/ping.php",
{target: $('#ping_target').val(),
num: $('#ping_num').val()
},
function(responseText) {
$("#results").html(responseText);
}
);
I've read the following questions,
- Incremental Output - Jquery and PHP
- How to show ping output dynamically in a web page?
- how to get live results with jquery?
and know that I could send x requests of 1 ping each instead of a single request for x pings, or that I could write the results of the ping to a temp file and fire off periodic requests to fetch the latest state of the file. These are all rather inelegant when the heart of the matter is a single ping command.
In short, the question is, "Can I fire off a single request, accept the resulting response stream, and update the contents of an element with new response data multiple times before it's finished?" It could update every time any data is received, update every x milliseconds, update after receiving each newline, etc.
From my reading the answer appears to be "no", but I'd like to confirm once and for all that this isn't possible with jQuery. (Followup: Is it possible to implement this single query capability without jQuery?)
Thank you!