Basic Ajax tutorial

Source: Internet
Author: User
Tags html validator mootools

Create an instance of the XMLHTTPRequest object (you only need to check whether the browser supports ActiveX controls)

VaR XMLHTTP;

Function createxmlhttprequest ()

{

If (window. activexobject)

{

XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");

}

Else if (window. XMLHttpRequest)

{

XMLHTTP = new XMLHttpRequest ();

}

}

XMLHttpRequest cannot request resources outside the domain where the script is located

XMLHttpRequest sends the request to the server

The send () method of the XMLHTTPRequest object sends the request to the specified target resource. The send () method accepts a parameter, usually a string or a DOM object. This parameter is sent to the target URL as part of the Request body.When providing parameters to the send () method, make sure that the method specified in open () is post.

If no data is sent as part of the Request body, null is used.

Generally, parameters submitted using Ajax are simple strings. You can directly use the get method to write the parameters to be submitted to the URL parameters of the open method, the parameter of the send method is null.

For example:

VaR url = "login. jsp? User = xxx & Pwd = xxx ";

XMLHttpRequest. Open ("get", URL, true );

Xmlhttprequset. Send (null );

You can also use the send method to pass Parameters. The post method is used to pass parameters using the send method. You need to set the Content-Type header and simulate the http post method to send a form so that the server can know how to process the uploaded content. The parameter submission format is the same as that in the get method. You must call the open method before setting the header information.

For example:

XMLHttpRequest. Open ("Post", "login. jsp", true );

XMLHttpRequest. setrequestheder ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8 ");

XMLHttpRequest. Send ("user =" + username + "& Pwd =" + password );

Note that, depending on the submission method,The two submission methods call the background doget method and dopost method respectively.

The benefit of adding a timestamp when requesting a URL:

In some cases, Some browsers cache the results of the XMLHttpRequest request to the same URL. however, although requests with the same URL have different results at different times. when the timestamp is appended to the end of the URL, the uniqueness of the URL can be ensured, so that the browser does not cache the results.

The post method of XMLHttpRequest can be used to send various content (XML and binary data) in the Request body)

JSON is a text format that is independent of a specific language and can serialize the data structure.

Best practices

: Each verification routine written on the client must be rewritten on the server in some way, because the user may disable javascript.

Read the corresponding Header

In some cases, we only want to read the response header sent by the server and ignore the content ., The standard practice for completing such a request is to use the head request instead of the get or POST request discussed earlier. When the server responds to the head request, it only sends the response header and ignores the content. Even if the requested content can be returned to the browser, it does not actually return the content. Because the content is ignored, the response to the head request is much smaller than the get or post response.

By reading the response header, you can obtain the Content-Type and Content-Length

(Content length), or even the last-modified (last modified) date.

Implementation Method: set the first parameter of open to "head". XMLHTTP. Open ("head", URL, true );

The getAllResponseHeaders () method has limited usage because it puts all Response Headers together for string return. To use the getAllResponseHeaders () method to obtain a single response header, You need to parse the returned string and find the response header that you are interested in.

The getResponseHeader method can solve this problem by returning only the value of one response header. This party

Obtain a string parameter, which indicates the name of the Response Header (that is, you want to get this response header ).

).

Firefox Tool

HTML validator finds and marks an error on the HTML page
Webdeveloper
Greasemonkey
Venkman
Other tools

Jslint (web page) completes JavaScript syntax check
Jsunit (JavaScript unit test) is used to test JavaScript.

Ajax framework

Qooxdoo, jquery, mootools, dojo, ext JS

No1-Prototype
No2-Dojo
No3-Mootools
No4-JQuery
No5-Dwr
No6-Buffalo
No7-Yui
No8-Ext
No9-Qooxdoo
No10-Spry

Who is the best Ajax framework in your mind? -- Csdn topics

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.