Two Methods for ajax to open a request (get, post)

Source: Internet
Author: User

Ajax has three methods to open services: get, post, and head.
Head is mainly used to obtain information about some header files on the server, such as charset and cont-type.
The first two methods are discussed here, which are frequently used in practice.

1. get Method
The get method is the most common. Generally, the get method is used for user logon and password modification.

1. Create an html document. The body TAG content is as follows:

<Body style = "text-align: center">
<Input type = "text" id = "txt"/>
<Br/>
<Input type = "button" value = "get method callback" onclick = "get ()"/>
</Body>

2. js code file

Var xhr = getXHR (); // get the xmlhttprequest object. The specific implementation of the getXHR function is not provided here, because it is very simple.

Function get ()
{
Var str = document. getElementById ("txt"). value;
Var url = "PageAjax. aspx? Argument = "+ escape (str); // code str
Xhr. open ("get", url, true );
Xhr. onreadystatechange = renew;
Xhr. send (null); // NO content is sent because the url contains parameter data.
}
Function renew ()
{
If (xhr. readystate = 4)
{
If (xhr. status = 200)
{
Var response = xhr. responsetext;
Var res = response. split ('\ n ');
Alert (res [0]);
}
}
}

3. The code for the PageAjax. aspx. cs file on the server side is as follows:

Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["argument"]! = Null)
{
String res = "Successful post callback! The input parameters are: "+ Request [" argument "]. ToString () +" \ n ";
Response. Write (res );
}
}

4. A simple get callback is completed.

Ii. post Method
Because parameters such as the user name and password must be input to the url address every time in get mode, the transmission mode can be considered because of the few characters, but when there are many parameters and the string value of the parameter is very long (for example, you cannot pass the content of the entire blog to the url in the form of parameters), this method is not good, because of the post method.

1. Create an html document. The body TAG content is as follows:

<Textarea id = "TextArea1" style = "width: 323px; height: 76px"> </textarea>
<Br/>
<Input id = "Button1" type = "button" value = "post callback" onclick = "post ()"/>

2. js code file

Var xhr = getXHR (); // get the xmlhttprequest object. The specific implementation of the getXHR function is not provided here, because it is very simple.
Function post ()
{
Var str = document. getElementById ("TextArea1"). value;
Var poststr = "arg =" + str;
Var url = "PageAjax. aspx? Time = "+ new Date (); // Add a timestamp. The data sent back is the data cached by the server.
Xhr. open ("post", url, true );
Xhr. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded"); // tell the server to send text
// Xhr. setRequestHeader ("Content-Type", "text/xml"); // tell the server that an xml file is sent
Xhr. onreadystatechange = update;
Xhr. send (poststr); // send poststr data to the server
}
Function update ()
{
If (xhr. readystate = 4)
{
If (xhr. status = 200)
{
Var response = xhr. responsetext;
Var res = response. split ('\ n ');
Alert (res [0]);
}
}
}

3. The code for the PageAjax. aspx. cs file on the server side is as follows:

Protected void Page_Load (object sender, EventArgs e)
{
If (Request ["arg"]! = Null)
{
String res = "successful get callback! The input parameter is: "+ Request [" arg "]. ToString () +" \ n ";
Response. Write (res );
}
}

4. A simple post callback is completed.

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.