Ajax结合CGI

本文介绍了如何利用Ajax实现客户端与服务器之间的高效数据交互。通过示例代码展示了如何创建XMLHttpRequest对象并发送GET请求,同时提供了CGI端的响应处理方式。文章详细解释了请求与响应的过程,并给出了具体的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

发现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;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值