In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about how to use ajax for asynchronous communication under boa server. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The main realization and trampling pits are as follows:
1. Get or post request: how to make a request see the ajax tutorial on W3School
It is recommended that people who do not have a front-end foundation use get requests, because you only need to make a string concatenation in the request parameters to complete the basic ajax requests. For specific implementation, you can refer to this URL (http://blog.csdn.net/huguohu2006/article/details/7755107), next focuses on post requests. I won't talk about the advantages here, which are found in the previous tutorials, mainly about how to implement them:
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); xhr.responseText as an invalid return value under return returnValue;// firefox / / ie can use return to get the value. However, firefox cannot. It can only handle a function when readyState = = 4 & & status = = 200. This function should be passed into the function as an argument. There is a strange phenomenon that if you remove the comments in the red line, firefox can get the value again. / / it is estimated that there is a delay in the use of ajax values by firefox. / / return xhr.responseText.toString ();}}; xhr.open ("post", url, true); / / send (string) is only applicable to post request xhr.send (data);} else {/ / XMLHttpRequest object creation failed alert ("browser does not support, please change browser!") ;}}
Call the sender function to implement ajax. The two parameters of the function are the url of the request and the data to be sent. Note that the post request can only send data of type string. If you want to send other types of data, it is recommended to use the ajax method encapsulated by jquery. Here, the native ajax method is used to send data for the following reasons:
Because of the large size of the jquery library, it is possible to fail when mount enters the linux system on the development box. This situation can be solved by mount u disk. The commands of mount u disk are as follows: mount-r / dev/uba4 / web-r is the read and write permission of the mount file, and specific search query can be executed. Uba4 is the name displayed by the U disk on the linux system, and web is the target folder. The disadvantage of using USB disk to mount is that all the files of the whole USB disk will be copied to the destination folder, which is somewhat redundant.
There is not much data sent, and there are no other type requirements. The use of string types can fully meet the development needs, so there is no need to introduce jquery libraries to increase project space.
Native ajax can better explain the principle of http request.
Here's how the cgi file handles the http request. The example letter is as follows:
# include # include # include char* get_cgi_data (FILE* fp, char* method) {char* input;int len;int size=1024;int iTun0 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] ='\ 0percent return input;} while (1) {input [I] = (char) fgetc (fp); if (I = = size) {input [item1] ='\ 0percent return input;}-len;if (feof (fp) | | (! (len) } int main (void) {char* input;char* method;char name [64]; char passwd [64]; int iDeposit int jint0politics 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 method of dealing with two kinds of requests in c language. Get request is relatively simple. You can get the data sent by the request directly using getenv ("QUERY_STRING"), while the post request is more responsible. It first obtains the content length of the request, and then dynamically allocates a string space of the same length according to the length, and then transmits the sent data to the string, and then carries on the corresponding processing according to the needs of your own project.
Thank you for reading! This is the end of the article on "how to use ajax to carry out asynchronous communication under the boa server". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.