Ajax结合CGI

来源:互联网 发布:剑三军娘脸型数据 编辑:程序博客网 时间:2024/05/01 21:08

发现Ajax真是好东东:可以只传XML单纯的客户端想要的数据..

客户端Ajax代码:

<script language="JavaScript" type="text/JavaScript">
<!--

    //向服务器发起XMLHTTP请求。
    var http_request = null; 
    function GetData()
 {
        //开始初始化XMLHttpRequest对象  
        if(window.XMLHttpRequest)              //Mozilla 浏览器 
  {   
            http_request = new XMLHttpRequest();  
            if (http_request.overrideMimeType) {//设置MiME类别  
                http_request.overrideMimeType('text/xml');  
            }  
        }  
        else if (window.ActiveXObject)         // IE浏览器
  {    
            try
   {  
                http_request = new ActiveXObject("Msxml2.XMLHTTP");  
            }
   catch (e)
   {  
                try
    {  
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");  
                }
    catch (e) {}  
            }  
        }  

        // 异常,创建对象实例失败
        if (!http_request)
  {  
            window.alert("不能创建XMLHttpRequest对象实例.");  
            return false;  
        }  

        http_request.onreadystatechange = processResponse; 
 
        // 确定发送请求的方式 
        http_request.open("GET", "../cgi-bin/status.cgi", true);  
        http_request.send(null);  
    }  

    // 处理返回信息的函数  
    function processResponse()
 {  
        if (http_request.readyState == 4)       // 判断对象状态
  {  
            if (http_request.status == 200)    // 信息已经成功返回,开始处理信息
   {   
                var result = http_request.responseText;
                if(result.indexOf("#") != -1)
    {
     show(result);
                }  
                else
    {  
                }  
            }
   else    //页面不正常
   {   
                alert("您所请求的页面有异常。");  
            }  
        }  
    } 

CGI端可以采用CGIC库(不错的一个库,不过Main()在库里面,把Main()改为一个普通接口最好):

#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>

int ShowInfo();

int cgiMain()
{
 cgiHeaderContentType("text/html");             //不能少

 /* Now show the form */
  fprintf(cgiOut, "1#2#3#",szName);


 return 0;
}

 

原创粉丝点击