[Recommended] common Ajax get and post Methods

Source: Internet
Author: User

Learn Ajax tutorials and learn more about get and post

GetMethod:
Function: Transfers simple data.
Size: The maximum URL length is 2083 bytes. The length of data transmitted by get is 2048 bytes.
Body: append data to the URL for sending, that is, HTTP header Transfer

PostMethod:
Function: Transfers simple and complex data.
Size: Limited in Web. config
Body: data is transmitted in the object content of the HTTP request.

Ajax transmits data in post mode. Note the following:
1. Set the context-type of the header to application/X-WWW-form-urlencode to ensure that the server knows that there are parameter variables in the object.
SetRequestHeader ("Context-Type", "application/x-www-form-urlencoded ;")
2. The parameters are key-value pairs with one-to-one names and values separated by an ampersand (&). For example, name = ABC & sex = Man & age = 18.
3. the parameter is sent in the send (parameter) method.
4. server Request Parameters are differentiated between get and post. for example, in Asp.net, request. form ["name"] Request Parameters in the object. in this case, the URL parameter request is. querystring ["name"] will cause an exception

 

<JavaScript type = "text/JavaScript" Language = "JavaScript">
Function createxmlhttp ()
{
If (window. activexobject)
{
Return new activexobject ("Microsoft. XMLHTTP ");
}
Else if (window. XMLHttpRequest)
{
Return new XmlHttpRequest ();
}
}

// Configure //---------------------------------------------------------------------------------------------------------------------

// Post method:

Function Start ()
{
Var ParamString = "name = abc & sex = man & age = 18 ";
Var XmlHttpObject = CreateXmlHttp ();
XmlHttpObject. onreadystatechange = StateEvent;
XmlHttpObject. open ("post", "test. aspx", true );
XmlHttpObject. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded;"); // sets the Server Response Request body parameters

XmlHttpObject. send (ParamString );

}

// Configure //---------------------------------------------------------------------------------------------------------------------

// Get method:

Function Start ()

{

Var XmlHttpObject = CreateXmlHttp ();

XmlHttpObject. onreadystatechange = StateEvent;

XmlHttpObject. open ("get", url, true );

XmlHttpObject. send (null );

}

// Configure //---------------------------------------------------------------------------------------------------------------------

// Create a callback function

Function StateEvent ()
{
If (http_request.readyState = 4) // server response status

{

If (http_request.status = 200 | http_request.status = 304) // code execution status

{

Var resStr = http_request.responseText;

Alert (resStr); // process the business logic here

}

Else

{

Alert ("the page you requested has an exception! ");

}

}

Else

{

SelectedResTits. innerHTML = " ";

}

}

</Script>

 

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.