[Ajax Study Notes] Ajax Server Response

Source: Internet
Author: User

In the previous [Ajax study note] Ajax preliminary test, I briefly learned about Ajax.

I learned this by referring to Ajax. CHM, and the resources have been uploaded. Reference link: Ajax topic

Today I learned some about Server Response in Ajax.

In Ajax, the server's response is to present the data returned by the server to the page through the response function. The server response is reflected in the server response callback function.

In the previous example, the response function is:

Function updatepage () {If (XMLHTTP. readystate = 4) {// HTTP ready state if (XMLHTTP. status = 200) {// identify the HTTP status code var response = XMLHTTP. responsetext; document. getelementbyid ("Ajax-result "). value = response;} else if (XMLHTTP. status = 404) {alert ("request URL does not exist! ") ;}Else {alert (" error: status code is "+ XMLHTTP. Status );}}}

The response function should be described as follows:

1. XMLHTTP. readystate

In this example, XMLHTTP is an instance of the XMLHTTPRequest object. This object has a readystate attribute, indicating the ready state.

There are five readiness statuses:

> 0: the request has not been initialized (open () has not been called ())

> 1: The request has been created but has not been sent (send () has not been called ())

> 2: The request has been sent and is being processed. (You can obtain the Content Header from the response)

> 3: The request is being processed. Generally, some data in the response is available, but the server has not yet completed the corresponding generation.

> 4: The response has been completed, indicating that the server response can be obtained and used.

It can be seen from these states that when the readystate object of the XMLHTTPRequest object is 4, it indicates that the server has completed the response to the request, and the user can perform operations on the server response data, therefore, the callback function displays code that determines XMLHTTP as 4.

Try: If you write this in the response function:

function updatePage(){alert("readyState=" + xmlHttp.readyState);}
You will find that the browser will pop up 2 ~ The alarm is triggered four times to indicate the current readiness status. This is because the callback function is called every time the status changes.

2. XMLHTTP. Status

XMLHttpRequest has a status attribute to indicate the HTTP status code. (You must have encountered "404 Not Found" when visiting the webpage. the status code here is a number like 404, which indicates the HTTP response status of the server, 404 indicates that the request failed. 200 indicates that the request succeeded. If you view the status code, you can use the Web Developer tool to check the network column)

If you are interested, you can learn more about the HTTP status code.

The HTTP status code details in the callback function can increase the robustness of the program, and help the user to quickly locate the problems that occur in the program.

3. Ajax and XML

In the preceding callback function example, the responsetext attribute of XMLHttpRequest is used to indicate the text information returned by the server response.

There may be a misunderstanding here: From the name perspective, XMLHttpRequest data transmission should be in XML format. Actually not. Ajax data transmission can be text (like the get method in the above example), or XML documents (using the responsxml attribute of XMLHttpRequest ).

For the web application client, data content transmitted using text transfer and XML is the same. Based on the same content, XML is a waste of space than common text, it is a waste of time during transmission, so it is better for the client to transmit data using plain text in most cases.

Why does XML increase processing time?

When transmitting data using XML: Wrap the text into XML; send additional information (note that I do not contain any surrounding elements, XML headers, or anything else that may appear in the actual request ); let the server Parse XML, generate a response, wrap the response in XML, and send it back to the webpage. Let the webpage parse the response and use it.

However, XML is used in some cases. For example, if the get method is used to send name/value pairs, if the data volume is large and the get method has a certain length limit, XML can be used for transmission.

For the Web application server, sending XML to the server is roughly divided into two types:

1> the server only accepts XML requests.

2> you are calling a Remote API that only accepts XML or SOAP requests. (It can be seen as a special case of the previous situation)

To send XML data, two steps are required to ensure that XML data can be correctly received:

1> make sure that the XML script sent to it can accept the XML data format.

2> ensure that the script recognizes the specific XML format and structure used to send data.

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.