LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-27-2007, 11:40 PM   #1
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Rep: Reputation: 15
Parsing reponseText from C++ to Javascript - AJAX


I can't parse my output from a .cpp file. can someone help me with the responseText and how I am using response[?].

Code:
function getServerHealth()
{
	var req = null;
	var url;
	var response;
	var info = new Array();
	var response;
	var i;
	if (window.XMLHttpRequest)
		req = new XMLHttpRequest();
	else if (window.ActiveXObject)
		req = new ActiveXObject(Microsoft.XMLHTTP);

	url = "serverHealth.cgi";

	req.open("GET", url, true);

	req.onreadystatechange = function()
	                         {
		                         if(req.readyState == 4)
		                         {
			                         if(req.status == 200)
			                         {
				                         response = req.responseText.split(" , ");
				                         document.getElementById("topOutput").innerHTML= response[0];


				                         for(i = 2 ; i < response.length; i++)
				                         {
					                 
					                        document.getElementById("bottomOutput").innerHTML =+ response[i];
				                         }

			                         }
		                         }
		                         //else
		                         //{
			                   //      document.getElementById('bottomOutput').value =
			                   //          "Error: returned status code " + req.status + " " + req.statusText;
		                         //}
	                         }

	                         req.send(null);
	//Recursive JavaScript function calls getDateTime() every second
	setTimeout("getServerHealth()", 5000);
}

Last edited by calorie712; 04-28-2007 at 02:52 PM.
 
Old 04-28-2007, 02:51 PM   #2
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Maybe posting my C++ code would help

Code:
/* The "intelligence" for the serverHealth program. */
#include <string>
#include <iostream>

using namespace std;

#include <stdio.h>
#include <unistd.h>


int main() {
  FILE *read_fp, *read_fp2;
  char buffer[BUFSIZ + 1];
  string mybuffer, str;
  int chars_read;
   
  cout << "Content-Type: text/html\n\n" << endl; 

  memset(buffer, '\0', sizeof(buffer));

  read_fp = popen("uptime", "r");
  if (read_fp != NULL) {
    chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
    if (chars_read > 0) {
      mybuffer.assign(buffer);
	  mybuffer.append(" , ");
	  cout << mybuffer << endl;
    }
    pclose(read_fp);
  }
  
read_fp2 = popen("tail -5 /var/www/logs/error_log", "r");
  if (read_fp2 != NULL) {
    chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp2);
    if (chars_read > 0) {
      str.assign(buffer);
	  cout << str << endl;
    }
    pclose(read_fp2);
  }
   return 0;
}
 
Old 04-28-2007, 02:53 PM   #3
calorie712
LQ Newbie
 
Registered: Feb 2006
Posts: 26

Original Poster
Rep: Reputation: 15
Might as well put the HTML in too!

Code:
<HTML>
<HEAD><TITLE>Server Health page</TITLE>
   <SCRIPT LANGUAGE="JavaScript" SRC = "serverHealth.js">
</HEAD>
<BODY BGCOLOR = "White">
  <P> This page will show the Health of the server</P>
  <FORM NAME = "AJAX" METHOD = "POST" ACTION = "">
  <INPUT TYPE = "button" value = "Get Health" onClick = "getServerHealth();">
  <P><B> Uptime output:</B><BR>
  <TEXTAREA ID = "topOutput" ROWS = "3" COLS = "80" WRAP = "Virtual"></TEXTAREA><P>
  <TEXTAREA ID = "bottomOutput" ROWS = "20" COLS = "80" WRAP = "Virtual">
  </TEXTAREA><BR>
  </FORM>
</BODY>
</HTML>
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Learning Javascript and Ajax LXer Syndicated Linux News 0 04-24-2007 02:16 PM
LXer: JavaScript and Ajax for the Web, Sixth Edition (Visual QuickStart Guide) LXer Syndicated Linux News 0 11-22-2006 02:54 AM
LXer: Build Ajax fuctions without Javascript coding LXer Syndicated Linux News 0 10-19-2006 12:21 AM
LXer: The OpenLaszlo Javascript advantage over normal Flash and Ajax LXer Syndicated Linux News 0 02-23-2006 02:46 AM
LXer: Make asynchronous requests with JavaScript and Ajax LXer Syndicated Linux News 0 01-22-2006 04:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration