AJAX post or get server request

AJAX post or get server request:

XMLHttpRequest object is used to exchange data with the server.

If you want to send the request to the server, you need to use theopen()andsend()methods of the XMLHttpRequest object.

Properties Description
##open(method,url,async) Specifies the request type and URL And whether to handle the request asynchronously.(1).method: type of request; GET or POST.
(2).url: The location of the file on the server.
(3).async:true (asynchronous) or false (synchronous).
send(string) Sends the request to the server.string: Only used for POST requests

1. The difference between get and post:

The get method may be faster and has strong applicability, but in many cases it is still necessary to use post.

The recommended scenarios for using the post method are as follows:

(1). It is not expected to use cached files (updating files or databases on the server).

(2). Send a large amount of data to the server (POST has no data limit).

(3). When sending user input containing unknown characters, POST is more stable and reliable than GET.

2. Get method:

First look at a get method code:

     php中文网  

In the above code, click The button can get the current date and time of the server. The C# code is as follows:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demo : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { Response.Write(System.DateTime.Now); } } }

Special note: The above method may read data from the cache in the IE browser, which means that the time will be obtained normally when the button is clicked for the first time. After the date, subsequent clicks will have no effect. There is no such problem in browsers such as Google or Firefox. The solution is as follows:

     php中文网  

The solution is very simple, which is to add a random number after the url. That's it, so that each request is considered a new URL, so it won't be cached.

You can also use the GET method to send information. You can send information through url. The code is as follows:

     php中文网  

Click the button to get the specified content. The following is the C# code:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace ajax { public partial class demoPara : System.Web.UI.Page { string webName; int age; protected void Page_Load(object sender, EventArgs e) { webName =Server.UrlDecode(Request.QueryString["webName"]); age = Convert.ToInt32(Request.QueryString["age"]); Response.Write("欢迎来到" + webName + ",本站已经成立" + age + "周年。"); } } }

3. POST request:

Look at a code example:

     php中文网  

The above code uses the post method to obtain the current time and date of the server, which does not exist Caching problem using get method.

If you need to POST data like an HTML form, you can use setRequestHeader() to add HTTP headers, and then specify the data to be sent in the send() method:

     php中文网  
Continuing Learning
||
php中文网
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!