ASP. NET Ajax client programming journey (search this title for a series of good articles)

Source: Internet
Author: User

Window. activexobject: return a bool value to check whether the browser supports ActiveX controls.

 

3 // obtain the XMLHTTPRequest object for different browsers
4 function createxmlhttprequest ()
5 {
6 if (window. activexobject)
7 {
8 XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
9}
10 else
11 {
12 XMLHTTP = new xmlhttpresquest ();
13}
14}

16 // click the JS function called when btnsayhello
17 function btnsayhello_onclick ()
18 {
19 createxmlhttprequest ();
20 XMLHTTP. onreadystatechange = handlestatechange;
21 XMLHTTP. Open ("Post", "output. aspx", true );
22 XMLHTTP. Send (null );
23}
24
25 // callback function
26 function handlestatechange ()
27 {
28 If (XMLHTTP. readystate = 4)
29 {
30 if (XMLHTTP. Status = 200)
31 {
32 document. getelementbyid ("result"). innerhtml = XMLHTTP. responsetext;
33}
34}

35}

 

Next I will give a brief explanation of this Code. First, XMLHTTP is a global variable. After initialization, it is responsible for the core of the entire Ajax application. The createxmlhttprequest method is used to obtain an instance of the XMLHTTPRequest object. We note that because different browsers have different methods to obtain objects, we need to first check the browser type and then use the corresponding method to obtain objects, which is 10 minutes of trouble. Btnsayhello_onclick is a core function that is called when a button is clicked. It first initializes XMLHTTP, then specifies the callback function as handlestatechange, and finally asynchronously releases the POST request to call the output. ASPX page. Finally, handlestatechange is called automatically when the Request status of XMLHTTP changes. In this function, when the status is normal, set the content of the DIV with the ID of "result" as the call result. That is, the string output by output. aspx.

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.