Ajax-goahead局部刷新页面

    软件开发最常用的方法是:C/S,B/S。如果嵌入式设备中使用Ajax,那么既可以使用C/S方式,也可以使用B/S开发上位机。最近公司的一个项目需要异步获取后台数据,使用form更新数据时会有空白卡顿不会很流畅,需要重新加载整个页面。在网上搜了一些资料,发现没有封装好的函数(本来想偷懒呢),都是简单介绍了一下用法。下面是自己封装的goahead-2-5中Ajax 的函数,可以直接使用的;可以在go-ahead 中使用cJson 对上通信。嵌入式设备中cJson/xml/html 三种数据格式,cjson 的代码只有 1000+ 行, 而且只是简单的几个函数的调用,相对于其他两种更节省资源,更易使用;

  goahead-2-5中Ajax 方法的封装类似于form,Asp的封装,用法相对于前两种更简单

  1  
  2  /********************************** Description *******************************/
  3  
  4  /*
  5   *    This module implements the /ajax handler. It emulates CGI processing
  6   *    but performs this in-process and not as an external process. This enables
  7   *    a very high performance implementation with easy parsing and decoding 
  8   *    of query strings and posted data.
  9   */
 10  
 11  /*********************************** Includes *********************************/
 12  
 13  #include    "wsIntrn.h"
 14  
 15  /************************************ Locals **********************************/
 16  
 17  static sym_fd_t    ajaxSymtab = -1;            /* Symbol table for ajax handlers */
 18  
 19  
 20  /************************************* Code ***********************************/
 21  static int websAjaxHandlerDone(webs_t wp);
 22  static void websAjaxHeader(webs_t wp);
 23  /************************************* globle ***********************************/
 24  /*
 25   *    Process a ajax request. Returns 1 always to indicate it handled the URL
 26   */
 27  int websAjaxHandler(webs_t wp, char_t *urlPrefix, char_t *webDir, int arg, 
 28      char_t *url, char_t *path, char_t *query)
 29  {
 30      sym_t        *sp;
 31      char_t        ajaxBuf[FNAMESIZE];
 32      char_t        *cp, *ajaxName;
 33      int            (*fn)(void *sock, char_t *path, char_t *args);
 34  
 35      a_assert(websValid(wp));
 36      a_assert(url && *url);
 37      a_assert(path && *path == '/');
 38  
 39  /*
 40   *    Extract the ajax name
 41   */
 42      gstrncpy(ajaxBuf, path, TSZ(ajaxBuf));
 43      if ((ajaxName = gstrchr(&ajaxBuf[1], '/')) == NULL) {
 44          websError(wp, 200, T("Missing ajax name"));
 45          return 1;
 46      }
 47      ajaxName++;
 48      if ((cp = gstrchr(ajaxName, '/')) != NULL) {
 49          *cp = '\0';
 50      }
 51  
 52  /*
 53   *    Lookup the C ajax function first and then try tcl (no javascript support 
 54   *    yet).
 55   */
 56      sp = symLookup(ajaxSymtab, ajaxName);
 57      if (sp == NULL) {
 58          websError(wp, 404, T("Ajax %s is not defined"), ajaxName);
 59      } else {
 60          fn = (int (*)(void *, char_t *, char_t *)) sp->content.value.integer;
 61          a_assert(fn);
 62          if (fn) {
 63  /*
 64   *            For good practice, ajaxs must call websDone()
 65   */
 66              websAjaxHeader(wp); 
 67              (*fn)((void*) wp, ajaxName, query); //调用实现函数
 68              websAjaxHandlerDone(wp); 
 69  
 70  /*
 71   *            Remove the test to force websDone, since this prevents
 72   *            the server "push" from a ajax>
 73   */
 74  #if 0 /* push */
 75              if (websValid(wp)) {
 76                  websError(wp, 200, T("Ajax didn't call websDone"));
 77              }
 78  #endif /* push */
 79          }
 80      }
 81      return 1;
 82  }
 83  /******************************************************************************/
 84  /*
 85   *    Define a ajax function in the "ajax" map space.
 86   */
 87  
 88  int websAjaxDefine(char_t *name, void (*fn)(webs_t wp, char_t *path, 
 89      char_t *query))
 90  {
 91      a_assert(name && *name);
 92      a_assert(fn);
 93  
 94      if (fn == NULL) {
 95          return -1;
 96      }
 97  
 98      symEnter(ajaxSymtab, name, valueInteger((int) fn), (int) NULL);
 99      return 0;
100  }
101  
102  /******************************************************************************/
103  
104  /*
105   *    Open the symbol table for ajaxs.
106   */
107  
108  void websAjaxOpen()
109  {
110      ajaxSymtab = symOpen(WEBS_SYM_INIT*2);
111  }
112  
113  /******************************************************************************/
114  /*
115   *    Close the symbol table for ajaxs.
116   */
117  
118  void websAjaxClose()
119  {
120      if (ajaxSymtab != -1) {
121          symClose(ajaxSymtab);
122          ajaxSymtab = -1;
123      }
124  }
125  /**********************************Local********************************************/
126  /*
127   *    Write a webs header. This is a convenience routine to write a common
128   *    header for a Ajax back to the browser.
129   */
130  
131  static void websAjaxHeader(webs_t wp)
132  {
133      a_assert(websValid(wp));
134      websWrite(wp, T("HTTP/1.0 200 OK\n"));
135  /*
136   *    By license terms the following line of code must not be modified
137   */
138      websWrite(wp, T("Server: %s\r\n"), WEBS_NAME);
139  
140      websWrite(wp, T("Pragma: no-cache\n"));
141      websWrite(wp, T("Cache-control: no-cache\n"));
142      websWrite(wp, T("Content-Type: text/html\n"));
143      websWrite(wp, T("\n"));
144  
145  }
146  /******************************************************************************/
147  /*
148   * ajax respone ok
149   */
150  static int websAjaxHandlerDone(webs_t wp)
151  {
152      websDone(wp,200);
153  }
View Code
用法:
1. 进程初始化时调用 websAjaxOpen();
  进程退出时调用 websAjaxClose();
2. 服务器端实现XMLHttpRequest应答:
注册Ajax handle函数:
  websUrlHandlerDefine(T("/ajax"), NULL, 0, websAjaxHandler, 0);
  注册实现方法:
websAjaxDefine("AjaxTest",AjaxTest);
3.实现方法:
int AjaxTest(char_t *name, void (*fn)(webs_t wp, char_t *path,
    char_t *query))
  {
    printf("data:%s\n",query); //接收到的数据
    websWrite("AjaxTest"); //发送的数据
  }
web端调用:
可以自己实现xmlhttprequest方法,也可以使用jq中的Ajax方法,本人使用的是jq中的方法:
 1    function HttpPostData(data){    
 2          //显示等待提示img
 3                $("#loadgif").show();  5                 $.ajax({
 4                    type: "POST",
 5                    contentType: "application/json",
 6                    url: "/ajax/AjaxTest",
 7                    data: data,
 8                    beforeSend:function() {
 9                        document.getElementById("Before").innerHTML = "正在更新数据请稍后......";   //等待提示字样                  
10                    },          
11                    complete: function () {
12              //接收完成
13                        $("#loadgif").hide();
14                                    },                                             
15                    error: function(){
16                           document.getElementById("result").innerHTML = "数据更新失败,稍后更新";      
17                          $("#loadgif").hide();                                    
18                                        },
19                    success: function(result) {
20               //接收数据
21                        document.getElementById("result").innerHTML=result;
22                    }
23                });
24    }
25 
26 //jq中Ajax使用的是utf-8编码格式,gbk方式会出现乱码,故goahead发送中文时需要进行转码;
27 //后面会更新C#中http的相关例程

 

 

希望各位博友多多指教 mail:1003278902@qq.com

 

posted @ 2016-04-24 11:21  雨点~  阅读(1722)  评论(0编辑  收藏  举报