Boa CGI Implementation Ajax

Source: Internet
Author: User

The main is to see an online blog http://blog.csdn.net/fdemon/article/details/6358520#quote, according to this blog does not achieve the correct results. The amendment is as follows:

1. Create an HTML Web page file. FileName: test.html

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
The application of <title>c+cgi+ajax in s3c2440 </title>
<script language= "JavaScript" src= "Xmlhttpreq.js" ></script>
<body>
<p> Server Current time is: <div id= "Current_time" ></div></p>
<input type= "button" value= "submit" onclick= "sender ()"/>
</body>

2. Create a JavaScript script file that implements Ajax for asynchronous access to the server. FileName: xmlhttpreq.js

/*
* Create an asynchronous Access object
*/
function Createxhr ()
{
var xhr;


Try
{
XHR = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (E)
{
Try
{
XHR = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (E)
{
XHR = false;
}
}

if (!xhr && typeof xmlhttprequest!= ' undefined ')
{
XHR = new XMLHttpRequest ();
}

return XHR;
}

/*
* Asynchronous Access Commit processing
*/
function Sender ()
{
XHR = CREATEXHR ();

if (XHR)
{
Xhr.onreadystatechange=callbackfunction;

Test.cgi followed by a cur_time parameter to prevent AJAX page caching
Xhr.open ("Get", "cgi-bin/test.cgi?cur_time=" + New Date (). GetTime ());

Xhr.send (NULL);
}
Else
{
XMLHttpRequest Object creation failed
Alert ("Browser not supported, please replace the browser.") ");
}
}

/*
* Asynchronous callback function processing
*/
function Callbackfunction ()
{
if (xhr.readystate = 4)
{
if (Xhr.status = 200)
{
var returnvalue = Xhr.responsetext;

if (returnvalue!= null && returnvalue.length > 0)
{
document.getElementById ("Current_time"). InnerHTML = returnvalue;
}
Else
{
Alert ("Result is empty.) ");
}
}
Else
{
Alert ("The page appears to be an exception.) ");
}
}
}

3. Set up server-side applications. FileName: test.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
time_t current;
struct TM *timeinfo;
Time (&current);
Timeinfo = LocalTime (&current);

This sentence must be added, otherwise there will be a page exception for asynchronous access
printf ("Content type:text/html\n\n");

printf ("%s", Asctime (Timeinfo));
}

Compile test.c on the terminal command line of Fedora 9, generating the test.cgi file as follows:

Arm-linux-gcc-o test.cgi test.c

4. Download test.html, xmlhttpreq.js Two files to the Development Board's WWW directory. (Note: The WWW directory is the document root directory of the BOA server.) You can configure the DocumentRoot node of the/etc/boa/boa.conf file in the Development Board to other directories, so that the three files are downloaded to your configured directory. test.cgi in the Cgi-bin directory This directory is the BOA CGI configuration directory, please be sure this directory of CGI can execute.

5. Connect the Development Board to the network, open the browser on the PC and enter: http://192.168.1.230/test.html (note: This IP address is your development Board IP, and this network segment to be consistent with the PC network segment), the operation effect, as shown:

After clicking on the Submit button, the server-side CGI program is accessed asynchronously to obtain the server-side system time. You can see the button to retrieve the system time after submitting the browser without refreshing. The Operation effect chart is as follows:

The above is copied some of the content of the user based on the change over.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.