Communication between ajax and cgi on The Boa Server and boaajaxcgi

Source: Internet
Author: User

Communication between ajax and cgi on The Boa Server and boaajaxcgi

Recently, we have made a most embedded course design, which requires a simple embedded webpage Interaction System Based on the IOT experiment Box Based on cortax a8 as the course design for acceptance rating. Because I am a preschool user, the webpage is not the focus, mainly communication with the boa Server, the example provided by the course experiment is that printf is directly used to print html tags to form a new page. People who have experience in front-end development know that this approach is inefficient and there is no way to Implement Asynchronous refresh, therefore, the blogger uses ajax for asynchronous communication under the boa Server.

The main implementation and pitfalls are as follows:

1. get or post requests: For more information about how to send requests, see the ajax tutorial on W3School.

We recommend that people without a front-end foundation use get requests, because you only need to concatenate a string in the Request Parameters to complete basic ajax requests, specific implementation can refer to this web site (http://blog.csdn.net/huguohu2006/article/details/7755107), next focus on post requests, here I will not talk about the advantages of the above, the previous tutorials are there, mainly talk about the implementation method:

Function sender (url, data) {var xhr = createXHR (); if (xhr) {xhr. onreadystatechange = function () {if (xhr. readyState = 4 & xhr. status = 200) {console. log (xhr. responseText); console. log (xhr. responseText. toString (); var returnValue = xhr. responseText. toString (); console. log (returnValue); return returnValue; // xhr in firefox. responseText is invalid as the return value. // you can use return to get the value of ie. But firefox cannot. You can only process a function when readyState = 4 & status = 200. // this function should be passed into the function as a parameter. There is a strange phenomenon that if you remove the comments of the red line, firefox can get the value again. // It is estimated that there is a delay in using ajax in firefox. // Return xhr. responseText. toString () ;}}; xhr. open ("post", url, true); // send (string) only applies to post request xhr. send (data);} else {// alert failed to create XMLHttpRequest object ("not supported by the browser. Please change the browser! ");}}

Call the sender function to implement ajax. The two parameters of the function are the request url and the data to be sent. Note that the post request can only send string data. If you want to send other types of data, we recommend that you use the ajax method encapsulated by jquery. The reasons for using the native ajax method to send data are as follows:

• The jquery library is large in size and may fail to be mounted to the linux System in the Development Box. In this case, you can mount the USB flash disk by using the following command: mount-r/dev/uba4/web-r is the read/write permission for the mounted file. For specific search queries, uba4 is the name displayed on the linux system, web is the target folder. The disadvantage of using a USB flash drive is that all the files on the USB flash drive will be copied to the target folder.

• A small amount of data is sent, and there are no other types of requirements. Using the string type can fully meet development requirements. There is no need to introduce the jquery library to increase project space.

• Native ajax can better explain the principles of http requests

The following describes how to process http requests using cgi files. The example is as follows:

#include <stdlib.h>#include <stdio.h>#include <string.h>char* get_cgi_data(FILE* fp, char* method){char* input;int len;int size=1024;int i=0;if (strcmp(method, "GET") == 0) /**< GET method */{input = getenv("QUERY_STRING");return input;}else if (strcmp(method, "POST") == 0) /**< POST method */{len = atoi(getenv("CONTENT_LENGTH"));input = (char*)malloc(sizeof(char) * (size+1));if (len == 0){input[0] = '\0';return input;}while (1){input[i] = (char)fgetc(fp);if (i == size){input[i+1] = '\0';return input;}--len;if (feof(fp) || (!(len))){i++;input[i] = '\0';return input;}i++;}}return NULL;}int main(void){char* input;char* method;char name[64];char passwd[64];int i=0;int j=0;printf("Content-type:text/html\n\n");printf("The following is query result:");method = getenv("REQUEST_METHOD");input = get_cgi_data(stdin, method);printf("string is: %s", input);return 0;}

The above contains two methods to process requests in C language. get requests are relatively simple. You can directly use getenv ("QUERY_STRING") to obtain the data sent by the request, post requests are more responsible for processing. First, get the length of the request content, and then dynamically allocate an equal-length string Space Based on the length to transmit the sent data to the string, then, you can perform corresponding processing based on the needs of your project.

PS: After the successful program printf is sent to the http request, it is the response received by the http request, that is, the value of the responseText attribute of the corresponding xhr. the reason for the c file is that arn-linux-gcc-o helloworld. cgi helloworld. c Name cross-compilation to obtain the corresponding. cgi File. The blogger then sends the next request after each successful request, because if the setInterval function is used directly for loop request of sensor data, it will produce a relatively large latency, it is basically equivalent to a process. If you store sensor data directly through a file, you can use the setInterval function.

The above is a full description of the ajax and cgi communication under the Boa Server. I hope it will help you. If you want to learn more, please stay tuned to the help house!

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.