Ajax under BOA server is associated with CGI communication _ajax

Source: Internet
Author: User
Tags html tags http request int size setinterval strcmp jquery library

Recently in the most have to do an embedded curriculum design, the request is utilizes the Cortax A8 The Thing Network experiment box to do a simple embedded webpage interactive system as the curriculum design to accept the grade. Because itself is to learn the front end, so part of the Web page is not the focus, mainly communication between the BOA server, the course experiment to give an example is the direct use of printf to print HTML tags to form a new page, with front-end development experience people know that this approach is inefficient and there is no way to achieve asynchronous refresh, So the blogger uses Ajax to carry out asynchronous communication under the BOA server.

The main realization and trampled pits are as follows:

1. Get or POST request: How to send request see Ajax tutorials on W3school

It is recommended that people who do not have a front-end base use the GET request because only a string concatenation of the requested parameters is required to complete the basic AJAX request, and the implementation can refer to this URL (http://blog.csdn.net/huguohu2006/article /details/7755107), next focus on the POST request, the advantages here I will not say, the previous tutorials are there, mainly about the implementation of the way:

function sender (URL, data) {
var xhr = createxhr ();
if (XHR) {
xhr.onreadystatechange = function () {
if (xhr.readystate = 4 && Xhr.status = =) {
Conso Le.log (xhr.responsetext);
Console.log (Xhr.responseText.toString ());
var returnvalue = xhr.responseText.toString ();
Console.log (returnvalue);
return returnvalue;
Firefox under Xhr.responsetext as the return value of the problem
//IE can be used to get the value of returns. But Firefox can not, only in readystate = = 4 && status = 200 processing a function
///This function should be passed as a parameter into the function. There's a strange phenomenon. If you remove the annotation in the Red Line section, Firefox can also fetch the value.
//estimated to be Firefox using AJAX value has a delay caused.
//return xhr.responseText.toString ();
}
;
Xhr.open ("Post", url, true);
Send (String) only applies to POST request
xhr.send (data);
} else {
//xmlhttprequest object creation failed
alert ("Browser not supported, Please change your browser! ");
}
}

Using the Invoke sender function to implement Ajax, the two parameters of the function are the URL of the request and the data to be sent, and note that post requests can only send string type of data. If you are sending other types of data that suggest a jquery-encapsulated Ajax approach, there are several reasons why native Ajax methods are used to send data:

The size of the jquery library is large, it is possible to mount the Linux system on the development box failure, this situation can be the Mount U disk to solve the Mount U disk command as follows: Mount-r/dev/uba4/web- R for Mount file read and write permissions, specific executable search query, UBA4 for u disk in the Linux system on the display of the name, the Web as the target folder, the use of U disk mount the disadvantage is that the entire U disk file will be copied to the target folder, there are more than dot

• Not many data sent, no other type requirements, use String type can fully meet development needs, no need to introduce jquery library to increase project space

• Native Ajax can better explain the principles of HTTP requests

The following is an introduction to the processing of HTTP requests by CGI files, as follows:

 #include <stdlib.h> #include <stdio.h> #include <string.h> char* GET_CG  I_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] = '; r
Eturn input; while (1) {input[i] = (char) fgetc (FP); if (i = = size) {input[i+1] = ' "; return input;}--len; if (feof (fp) | | (!
(len)))
{i++; Input[i] = ' n '; 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 the C language processing two kinds of request method, get request is relatively simple, direct use getenv ("query_string") can obtain to request to send the data, the POST request processing is more responsible, obtains the request content length first, Then according to the length of the dynamic allocation of a long string space, will send the data to the string, and then according to the needs of their own projects to deal with accordingly.

PS: The corresponding successful program to send HTTP request after printf is the corresponding to the HTTP request received, that is, the corresponding XHR responsetext attribute value, in addition, the. c file need reason Arn-linux-gcc-o helloworld.cgi HELLOWORLD.C named cross-compilation Gets the corresponding. cgi file. The blogger then continues to send the next request after each request succeeds, because if the setinterval function is used to iterate over the data of the sensor, it will produce a relatively large delay, essentially equivalent to the process, You can use the SetInterval function if you are storing sensor data directly through a file.

The above is a small set to introduce the BOA server under the Ajax and CGI communication of all the narration, hope to help everyone, if you want to learn more content please pay attention to cloud habitat community!

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.