[AJAX advanced (II)] -- asynchronous fuzzy query

Source: Internet
Author: User

The previous blog discussed the basic usage of DOM. The DOM tree allows programmers to directly operate HTML elements in the background, skillfully use DOM and construct a web application by integrating AJAX. Today, we will use AJAX to implement some complex functions. We will mainly discuss how to use AJAX to Implement Asynchronous fuzzy queries.

 

I. Check for missing vacancies

 

You may no longer be unfamiliar with AJAX attributes, because attributes and methods have been preliminarily discussed in the previous blog, and attributes and methods have been applied through instances, today, we will give a more in-depth introduction to how to use AJAX attributes.

1. attribute description

OnreadystatechangeIt is a very important attribute of AJAX. the literal translation is a request state change. It stores the functions that process server responses, that is, the register callback function. When you request data from the server, you must first execute this function and re-execute this attribute method after each request operation. The following code is usually used when writing browser scripts:

 

XmlHttp. onreadystatechange = function () {// we usually write the code here if (XMLHttp. readyState = 4) // determines whether the request operation is completed {if (XMLHttp. status = 200) // whether the data showDiv is returned correctly after the execution is complete. innerHTML = XMLHttp. responseText ;}}

 

 

In the preceding code example, an XMLHTTPRequest object is created and a callback function is registered for the object. The two attributes readyState and status are not mentioned. We will discuss them in detail below. First, let's take a look at the readyState attribute.

 

ReadyStateThe attribute stores the server response information. When the readyState changes, the onreadystatechange function is executed. You may ask why you want to judge this attribute, which means you do not have a deep understanding of this attribute. You can use this attribute to monitor the status of the returned request at all times, clearly understand the steps of the request. After the client sends a request, the server continuously sends data to the client. The value of this attribute changes every time data is returned. The following table lists the possible values of readyState.

ForStatusThe http status code returned by the server, as we often see in 404,500. They indicate different returned information. 200 indicates "successful", 404 indicates "not found", and 500 indicates "internal server error ". If you want to know more about its usage, you can apply this attribute more.

 

Ii. Fuzzy search

 

I have made a deeper discussion on several common attributes of AJAX to facilitate attribute programming. Next I made a Demo of fuzzy query, AJAX asynchronous query is used to query the Matching content of a keyword.

 

Listing 1: the client's html code. The following code example creates a text box and a div tag. The text box is used to enter the content, and the div displays the matched content. The requested server script is completed by a general handler and is not encoded for the sent content.

 

 <Script type = "text/javascript"> debugger; function GetData () {var showDiv = document. getElementById ("divTest"); var ctest = document. getElementById ("txtTest "). value; var XMLHttp = false; showDiv. innerHTML = ""; if (window. activeXObject) {XMLHttp = new ActiveXObject ("Microsoft. XMLHTTP ");} else if (window. XMLHttpRequest) {XMLHttp = new XMLHttpRequest ();} if (! XMLHttp & typeof XMLHttpRequest! = 'Undefined! ') {XMLHttp = new XMLHttpRequest ();} XMLHttp. onreadystatechange = function () {if (XMLHttp. readyState = 4) {if (XMLHttp. status = 200) showDiv. innerHTML = XMLHttp. responseText ;}} var url = "Process. ashx? Name = "+ escape (ctest) +" & id = "+ Math. random (); // Add a random number after the URL to prevent XMLHttp from being cached. open ("get", url, true); XMLHttp. setRequestHeader ("Content-Type", "text/xml"); XMLHttp. send (null) ;}</script> 
 

 

 

List 2: internal processing code of the Process. ashx server. Here, the database uses like to query a value similar to the input content, insert the value to the html table, return it to the client, and insert the data to HTML through showDiv. innerHTML.

 

Using System; using System. Collections. Generic; using System. Web; using System. Data; using System. Data. SqlClient; using System. Text; namespace WebApplication4 {////// Summary of Handler1 ///Public class Process: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/xml"; DataTable dt = new DataTable (); using (SqlConnection con = new SqlConnection ("server = .; dataBase = MyBlog; uid = sa; pwd = 1; ") {con. open (); // Open the database link SqlCommand sqlCom = new SqlCommand (); // declare and create the database command set StringBuilder sqlStr = new StringBuilder (); sqlStr. append ("select name fro M match where name like % 'text passed by the Client '% "); sqlCom. commandText = sqlStr. toString (); sqlCom. connection = con; SqlDataAdapter sqlDa = new SqlDataAdapter (sqlCom); SqlCommandBuilder sqlBuilder = new SqlCommandBuilder (sqlDa); sqlDa. fill (dt);} StringBuilder strBuild = new StringBuilder (); strBuild. append (""); if (dt! = Null & dt. rows. count> 0) {foreach (DataRow dr in dt. rows) {strBuild. append (""); strBuild. appendFormat ("", dr ["name"]. toString (); strBuild. append ("") ;}} strBuild. append ("
{0}
"); Context. Response. Write (strBuild. ToString ());}}}

 

 

Conclusion

 

This blog is mainly used to check for missing information, because the previous blogs did not have in-depth discussions on basic AJAX usage attributes, and the use of several attributes is still vague, this blog is mainly used to deepen understanding. So far, the basic content of AJAX has been discussed. I believe it is easy to use AJAX for programming after the discussion and application of these blogs. The next step is to use AJAX, apply what you learned to actual development.

 

Zookeeper

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.