index.html 未包含<script language="javascript" type="text/javascript" src="./js/ajaxbasic.js "></script>不能成功調用XHRGet函數。與此同時systemlanguage.html也成功調用XHRGet函數。javascript
這部份內容是對配置文件的保存css
cgi 裏的C語言裏邊的html
ucfg_config.xml ucfg_factory.xml 裏的文件看元素是否爲頂層元java
<language>
<ENABLE>1</ENABLE>
</language>linux
怎麼把文件一個一個讀出來?ajax
答:有專門的讀函數來讀取.json
cgi從服務器讀取數據的過程 1數組
#include<sys/ioctl.h> #include<sys/types.h> #include<sys/stat.h> #include<sys/socket.h> #include<sys/socket.h> #include<fcntl.h> #include<unistd.h> #include<netinet/in.h> #include<netinet/ether.h> #include<arpa/inet.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<net/if.h> #include<arpa/inet.h> #include<pthread.h> #include"cgicommon.h" #include"html-lib.h" #include"cgi-lib.h" #include"string-lib.h" #include"ini.h" #include "ucfg_lib.h" static pthread_cond_t cgi_cond = PTHREAD_COND_INITIALIZER; static pthread_mutex_t cgi_lock = PTHREAD_MUTEX_INITIALIZER; char langbuf[]={"aaaaa"};//定義字符串 static int GetLanguage(llist entries, int set) { if(set == 0) { CGI_XML_VALUE("LANGUAGE",langbuf); //langbuf字符數組首地址 LANGUAGE命令 } return 0; } int main() { list entries; int ret=0; int init_ucfg_ret=-1; char* command; list_create(&entries); CG8I_XML_BEGIN(); init_ucfg_ret=init_ucfg(); if(init_ucfg_ret<0){ CGI_XML_VALUE("Error", "%s", "init_ucfg Fail"); goto out; } ret = read_cgi_input(&entries); if(ret) { comand=cgi_val(entries, CGI_REQUEST_COMMAND); if(command==NULL) { CGI_XML_VALUE("Error","%s", "NoCommand"); } else if(!strcmp(command, "")) { pthread_mutex_lock(&cgi_lock); GetLanguage(entries,0); pthread_mutex_unlock(&cgi_lock); } else { CGI_XML_VALUE("Error","%s", "CGI Unkown Command"); } } out: if(init_ucfg_ret==0) deinit_ucfg(); CGI_XML_END(); list_clear(&entries); pthread_mutex_destroy(&cgi_lock); pthread_cond_destroy(&cgi_cond); return 0; }
從數組中讀取字符串數據,服務器
如果從服務器上讀取文件則修改框架
static void updateLanguageConf() { system("ucfg get config language > /tmp/language.conf"); //此處命令是linux下的命令 } static int GetLanguage(llist entries, int set) { char cmd_str[256]; char ini_file[256]; char get_value[LENGTH_INI_KEY_AND_VALUE] ={0,}; char *key; char ipaddress[32]; char val_set[32]; char *val; if(set == 0) { sprintf(ini_file,"/tmp/language.conf"); //服務器上的文本路徑 key="ENABLE"; //Ucfg_factory.xml Ucfg_config.xml 文件裏邊的屬性ENABLE <language> \n <ENABLE>0<ENABLE> \n <language> if(0 > GetPrivateProfileSring( "", //Nosection key, "0", get_value, sizeof(get_value) - 1, ini_file)) { return -1; } CGI_XML_VALUE("LANGUAGE",get_value); } return 0; } static int SetStatus(llist entries, int set) { char *val; val = cgi_val(entries, "LANG"); //此處是XHRPost()第三個參數var param = "Command=SetlanguageEnable" + "&LANG=" + autoexec; write_ucfg_xml(CONF_BLK,"/language/ENABLE",val); updateLanguageConf(); return 0; } int main() { llist entries; int ret=0; int init_ucfg_ret=-1; char* command; list_create(&entries); CGI_XML_BEGIN(); init_ucfg_ret=init_ucfg(); if(init_ucfg_ret<0){ CGI_XML_VALUE("Error","%s","init_ucfg Fail"); goto out; } ret = read_cgi_input(&entries); if(ret) { command = cgi_val(entries, CGI_REQUEST_COMMAND); if(command == NULL) { CGI_XML_VALUE("Error", "%s", "NoCommand"); } else if(!strcmp(command, "Language")) { pthread_mutex_lock(&cgi_lock); GetLanguage(entries, 0); pthread_mutex_unlock(&cgi_lock); } else if (!strcmp(command, "SetlanguageEnable")) //當js裏邊XHRPost()命令爲SetlanguageEnable { SetStatus(entries,0); //寫入函數被調用 } else { CGI_XML_VALUE("Error","%s","CGI Unknown Command"); } } out: 此處同上分代碼 }
以下來解析,此時經過SH,CGI_MXL_VALUE不能解析的三種格式,不支持json的格式須要轉換
CGI_MXL_VALUE(element,arg...)\
do{\
fprintf(stdout, "<%s>", element);\
printf(arg);
fprintf(stdout, "</%s>\n",element );\
}while(0)
js部分的代碼
function getLanguageResults(xmlDoc) { if(hasError(xmlDoc)) { showError(xmlDoc); return ; } var g_lang_info = getDataFromTag(xmlDoc, "LANGUAGE"); alert("info:"+g_lang_info); } function getLanuageHandless() { alert("bb"); var url="./cgi-bin/syslanguage.cgi?Command=Language"; 這裏的syslanguage.cgi && command必定要改對 XHRGet(getLanuageResults, url, false); alert("ccc"); } function initSystem() { alert("init"); getLanuageHandless(); } function initialize() { try { alert("firxt init"); top.initSystem(); initSystem(); } catch(E){} }
並經過wireshark, 搜索http 右鍵追蹤流 TCP流 中搜索Languge並查看CGI_MXL_VALUE函數的解析的數據。
有專門的讀函數來讀文件,
如下是寫函數的命令
系統命令ucfg get config languge ; ucfg get 是命令 languae是對象在ucfg_factory.xml ucfg_config.xml中定義
<language>
<ENABLE>1</ENABLE>
<language>
static void updateLanguageConf()
{
system("ucfg get config language > /tmp/language.conf");
};
寫入函數
static int SetStatus(llist entries, int set )
{
char *val;
val = cgi_val(entries, "LANG"); js的param裏邊的參數LANG
write_ucfg_xml(CONF_BLK,"/language/ENBALE",val); CONF_BLK 配置文件ucfg_config.xml的
<language>
<ENABLE>1</ENABLE>
<language>
/language/ENABLE language是頂層ENABLE子層,若果有多級別就類推
updateLanguageConf();
return 0;
}
js部分代碼
var param ="Command=SetlanguageEnable"+"&LANG="+ autoexec;
XHRPost(SetlanguageEnableHandler, "./cgi-bin/syslanguage.cgi", param, true);
以上是發送請求代碼
js代碼能夠返回頁面不能刷新,先刷新當前頁面location.reload();
當前頁面初始化時調用,刷新另外一個框架的頁面
//刷新另外一個框架的頁面用
<script language=JavaScript>
parent.另外一FrameID.location.reload();
</script>
parent.leftmenu.location.reload();
亦或top.location.reload();刷頂層頁面
整體思路網關的: html->js ->http等價於post->cgi等價於language.cgi ->寫入->language.conf ->language.conf->language.cgi->讀出->http等價於XHRGet ->JS->html.
boa服務器框構就是: html->js->http->cgi, 以上代碼是小型的http服務器,只要把協議搞清楚就好。