www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need Javascript to read CGI output

  1. #1
    Join Date
    Apr 2008
    Posts
    6

    Question Need Javascript to read CGI output

    I have a server with a .CGI on it than can be invoked by putting something like:
    http://172.16.0.78/variable.cgi?variable=current_julian

    into the address bar of a browser. This displays a single line of text that is the value of the requested variable, like 1230667891 in this case.

    I need to make a javascript on another server, that will access this .CGI on the first server, and rather than displaying the value, I want it to assign the output to a variable in my script.

    I've been trying things like:

    var cjul='<!--#exec cgi="http://172.16.0.78/variable.cgi?variable=current_julian"-->';

    But I'm not having any luck. Any ideas would be much appreciated.

  2. #2
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    You can use the XMLHttpRequest to get the data. You can try something like this...
    Code:
    <script type="text/javascript">
    	var openBodyTag = '<body>';
    	var closeBodyTag = '</body>';
    
    	function getText() {
    		new Ajax.Request('/path/to/cgi/script',
    				{
    					method: 'get',
    					parameters:{},
    					onSuccess: function(transport) {
    						html = transport.responseText;
    						try {
    							body = html.substr( (html.indexOf(openBodyTag) + openBodyTag.length), (html.indexOf(closeBodyTag) - (html.indexOf(openBodyTag) + closeBodyTag.length)));
    							alert(body);
    						} catch (err) {
    							alert(err);
    						}
    					},
    					onFailure:function(transport) {
    						alert("error getting cgi output");
    					}
    				}
    		);
    	}
    </script>
    oops, forgot to mention that the above uses the prototype library and could be more effective with regex
    Last edited by jasonahoule; 12-30-2008 at 05:29 PM. Reason: Oops

  3. #3
    Join Date
    Apr 2008
    Posts
    6
    Thank you for your reply, but I'm still having trouble. When I run this, I should always get an alert window, shouldn't I? That isn't happening. And if I try a line like

    document.write('<br>Text: '+html);

    after executing the function, I get an error message saying html is undefined.

    I'm sorry I don't know what regex is, but I did load the prototype framework v1.6.0.3 onto the server.

    Also, when I was trying to learn what Ajax is, I saw this on a tutorial:

    Did You Know? As a security measure, the XMLHTTPRequest object can generally only make calls to URLs within the same domain as the calling page and cannot directly call a remote server.

    This makes me wonder if this even has a chance of working. What do you think?

  4. #4
    Join Date
    Mar 2007
    Location
    Orlando
    Posts
    670
    Sorry, I missed the "another server" part. You could try using curl if you are using php.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles