Ajax creation and post and get requests
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Title: Methods and code examples to resolve 403 errors in jQuery AJAX requests. The 403 error refers to a request that the server prohibits access to a resource. This error usually occurs because the request lacks permissions or is rejected by the server. When making jQueryAJAX requests, you sometimes encounter this situation. This article will introduce how to solve this problem and provide code examples. Solution: Check permissions: First ensure that the requested URL address is correct and verify that you have sufficient permissions to access the resource.

jQuery is a popular JavaScript library used to simplify client-side development. AJAX is a technology that sends asynchronous requests and interacts with the server without reloading the entire web page. However, when using jQuery to make AJAX requests, you sometimes encounter 403 errors. 403 errors are usually server-denied access errors, possibly due to security policy or permission issues. In this article, we will discuss how to resolve jQueryAJAX request encountering 403 error

Build an autocomplete suggestion engine using PHP and Ajax: Server-side script: handles Ajax requests and returns suggestions (autocomplete.php). Client script: Send Ajax request and display suggestions (autocomplete.js). Practical case: Include script in HTML page and specify search-input element identifier.

How to solve the problem of jQueryAJAX error 403? When developing web applications, jQuery is often used to send asynchronous requests. However, sometimes you may encounter error code 403 when using jQueryAJAX, indicating that access is forbidden by the server. This is usually caused by server-side security settings, but there are ways to work around it. This article will introduce how to solve the problem of jQueryAJAX error 403 and provide specific code examples. 1. to make

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

PHP is a programming language widely used in website development, and page jumps and carrying POST data are common requirements in website development. This article will introduce how to implement PHP page jump and carry POST data, including specific code examples. In PHP, page jumps are generally implemented through the header function. If you need to carry POST data during the jump process, you can do it through the following steps: First, create a page containing a form, where the user fills in the information and clicks the submit button. Acti in the form

Title: PHP code example: How to use POST to pass parameters and implement page jumps In web development, it often involves the need to pass parameters through POST and process them on the server side to implement page jumps. PHP, as a popular server-side scripting language, provides a wealth of functions and syntax to achieve this purpose. The following will introduce how to use PHP to implement this function through a practical example. First, we need to prepare two pages, one to receive POST requests and process parameters

Ajax (Asynchronous JavaScript and XML) allows adding dynamic content without reloading the page. Using PHP and Ajax, you can dynamically load a product list: HTML creates a page with a container element, and the Ajax request adds the data to that element after loading it. JavaScript uses Ajax to send a request to the server through XMLHttpRequest to obtain product data in JSON format from the server. PHP uses MySQL to query product data from the database and encode it into JSON format. JavaScript parses the JSON data and displays it in the page container. Clicking the button triggers an Ajax request to load the product list.
