Home  >  Article  >  Web Front-end  >  Ajax creation and post and get requests

Ajax creation and post and get requests

php中世界最好的语言
php中世界最好的语言Original
2018-06-04 11:04:041336browse

This time I will bring you Ajax creation and post and get requests. What are the precautions for Ajax creation and use of post and get requests? The following is a practical case, let's take a look.

The process of creating AJAX:

XMLHttpRequest is the basis of AJAX and is used to exchange data with the server in the background, which means that certain parts of the webpage can be modified without reloading the entire webpage. Partially updated.

1. Create an XMLHTTPRequest object:

(兼容处理)var xhr = null;//前面必须添加window否则报错(不能拿一个不存在的对象作为判断条件)if(window.XMLHttpRequest) { 
    xhr = new XMLHttpRequest();
} else {

xhr = new ActiveXObject('Microsoft.XMLHTTP');
}2. Create a call to the server

xhr.open(method, url, async);
method: 请求数据类型(get, post, options, head, put, delete, trace, connect)

get Request:

xhr.send()

post request:

xhr.setRequestHeader("Content type", "application/x-www-form-urlencoded"); 
//向请求添加HTTP头xhr.send("fname=Bill&lname=Gates");

GET or POST?

Compared with POST, GET is simpler and faster, and can be used in most cases .

However, please use POST requests in the following situations:

Cache files cannot be used (updating files or databases on the server)

b. Sending large amounts of data to the server (POST has no data size limit)

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

url: The location of the file on the server

async: true (asynchronous) or false (synchronous)

Synchronization: means after sending the data, wait for the response to be received before sending the next data packet

Explanation: Submit the request->Wait for the server Processing -> After processing and returning, the client browser cannot do anything during this period; it can only do one thing at the moment, and other things must wait for the current thing to be completed before continuing.

Asynchronous: refers to issuing After receiving the data, there is no need to wait for the response to be received, and then the next data packet is sent.

Explanation: Submit the request ->Wait for the server to process (the browser can still do other things at this time) ->Processing completed; Half-hearted, You can do multiple things at the same time

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to avoid features and browser inference in JS

Polyfill annotations and preventing modifications in JS use

The above is the detailed content of Ajax creation and post and get requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn