How to use Ajax/jQuery to update table with XML output of server process?
I have an executable on my web server that outputs XML to a file or standard output. The XML contains data I would like to turn into a table. This XML will be unique per user and will change on a periodic basis.
On my web page, I would like to:
Trigger running this executable on a set period (e.g. 30 minutes) to refresh the XML data
Parse this XML and dynamically add/remove table rows as the开发者_StackOverflow社区 XML data changes between triggered runs
For #2, I think I can use $.ajax()
to grab the server-side XML file and work with its contents, using innerHTML
to render a table and add it to the web page.
I think the part I am stuck on is item #1. My web page is output from a Perl CGI script. Once the script is finished, the HTTP request is complete and there is no more communication between client and server.
Do I need to have a separate process running on the server that puts an XML file somewhere where the jQuery Ajax call can find it as needed? (I'd like to avoid this, as I'd have to manage many separate processes and XML files, depending on where the users are on the web site, whether they are logged in, etc.)
Or is there a cleaner way to have the client-side web page ask the web server to run a server-side executable, without reloading the web page? Something like a hidden form that, when triggered, can call an action without reloading the entire page?
Sorry if these are dumb questions. Thanks for your advice.
To be honest, I don't like the idea of using JQuery to perform the 30-minute polling because it is tightly dependent on the client-side then. If you have a user as dumb as me where I open your web page in 7 different browsers at once :D , then basically, this user is to going ask the server to execute the same Perl CGI script 7 times every 30 minutes. Further, if the user only visits the web page only for a few minutes, then JQuery will never have the opportunity to trigger that Perl CGI script.
If your script is not available as a web service (well, that would be ideal in the first place), the only feasible bet is to schedule your script to run every 30 minutes, say using a cron job. Sure, it is a separate process, and I hate that too, but at least you can ensure all users will have updated data every 30 minutes rather relying on your users to keep the data updated.
My 1.5 cents. :)
You can use jquery to execute a function every so often (say 30 mins) and make an ajax request to the server. In turn, the server will execute and request the XML file and push it to the javascript function where you can parse it. The way I understand your question, #1 is not an issue. Once the web page is rendered there is nothing that you would have to do.
like this:
window.setInterval(getXML, 1800000); //will execute the function every 30 minutes
function getXML() {
$.ajax({
type: 'POST',
url: http://yourserver.com/somepageorform,
data: data, //any data you may need to send
success: function(data) {
var url = data;
$.ajax({
type: 'GET',
url: url,
success: function(xml) {
//process the xml
},
dataType: dataType
});
},
dataType: dataType
});
}
This is an asynchronous call to your server. then somepageorform
will execute the cgi and generate the XML file. in the doSomething() function you would have the function that processes the XML as it will not execute until the process is done. I would assume the CGI would return the path to the file, so then in your function you would be able to get it and process it.
Your two options are to have a jQuery function on your page poll your server and check for changes or to have the server push the changes to your client.
To facilitate the first is trivial and there are plugins such as the refresh plugin that can help you poll the server. This is the easiest approach but it will mean you could possibly have a lot of redundant calls.
Alternatively you could use something like Pusher to push notifications to your client after the file is generated on your server. This is a little more complicated but will give you better control over when your updates are sent to clients.
精彩评论