Ajax entry (with full code) _ ax

Source: Internet
Author: User

Ajax is one of the technical features of Web2.0. After a long time, I finally have time to write it today. I am also in the initial stage. I have different opinions to discuss with you!

The general process is like this.
① Create an XMLHTTPRequest object first.
② Determine the Javascript script to be called when the object's status changes. to put it bluntly, what operations do you need to perform after the object communicates with the background. that is, what needs to be done after receiving the response from the server.
③ Send a request to the background. The accepted result (if any) obtained after the request is sent is operated in the script called in ②.
[Source code]
/* Add the following script to the page */

<Script language = "JavaScript" type = "text/JavaScript">
<! --
VaR XMLHTTP ; // Declare the XMLHTTPRequest object
// Create an XMLHTTPRequest object
Function createxmlhttprequest ()
{
If (window. activexobject) // For IE browser
{
XMLHTTP = new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest) // Non-IE browser
{
XMLHTTP = new XMLHttpRequest ();
}
Else
{
Return;
}
}

// MAIN PROCESS
Function startrequest ()
{
Creatxmlhttprequest ();// ①
XMLHTTP. onreadystatechange = handlestatechange;// ②
XMLHTTP. Open ("get", ax. aspx? Flag = "ax" & Email = "AXzhz@163.com", true );// ③ Parameter 1: The submission method is get,
XMLHTTP. Send ();// Parameter 2: Submitted page and included parameter 3: Asynchronous Communication
}

// The script to be called, that is, what needs to be done after receiving the response from the server.
Function handlestatechange ()
{
If (XMLHTTP. readystate = 4 ) // The response is complete.
{
If (XMLHTTP. Status = 200 ) // The response is normal/OK
{
/* After a normal response, write all the operations you want to do here */
VaR result = XMLHTTP. responsetext; // Obtain the background Return Value
Alert ("ajax operation completed, return value from the background" + result );
}
Else
{
Alert ("Sorry! \ N "+ XMLHTTP. statustext + XMLHTTP. status );
}
}
Else
{
Alert ("Sorry! Ajax has not completed the response !! ");
}
}
// -->
<SCRIPT>

/* The background part of the submitted page ax. aspx */
First: Put the page section of ax. aspx (htmlCodeOnly the first line of code is deleted, and then the background code is operated.

using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
namespace axzhang
{< br> ///


// initializesearch abstract description.
//
public class initialize: system. web. UI. page
{< br> private void page_load (Object sender, system. eventargs e)
{< br> // get the parameters passed by Ajax. Here I only get one of the parameters.
string result = request. querystring ["flag"]. tostring ();
result + = ": From ax. aspx ";
// return the result to the page
response. write (result);
}

# code generated by region web forms designer
override protected void oninit (eventargs e)
{< br> //
// codegen: this call is ASP.. NET web form designer.
//
initializecomponent ();
base. oninit (E );
}< br>
///


// The method required by the designer. Do not use the code editor to modify the method.
/// the content of this method.
//
private void initializecomponent ()
{< br> This. load + = new system. eventhandler (this. page_load);
}< BR ># endregion
}

Bytes ----------------------------------------------------------------------------------------------
Well, the entire Ajax request process is complete. Let's do it yourself. Don't just look at it!
Note: This is only a small part of Ajax applications .........
[Conclusion] The transmitted and received parameters seem to be only one string. Therefore, when multiple variables need to be transferred, they must be combined into one string, obtain the string and manually disassemble it ......I don't know who else can do anything.

I learned it under the guidance of this webpage,It is strongly recommended that new users go to the following link:
Http://www.bizwiki.cn/teamblog? P = 25

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.