Home Web Front-end JS Tutorial Detailed explanation of Ajax get, post and other methods in jQuery_jquery

Detailed explanation of Ajax get, post and other methods in jQuery_jquery

May 16, 2016 pm 04:19 PM
ajax get jquery post

load() method is usually used to obtain static data files from the web server, but this does not reflect the full value of ajax.

In the project, if you need to pass some parameters to the page on the server, you can use the $.get() or $.post() method (or the $.ajax() method)

$.get() method Uses GET method to make asynchronous requests. The structure is: $.get(url [, data] [, callback] [, type])

$.get() method parameters are explained as follows:

Parameter name Type Description
url String The URL address of the requested HTML page
data(optional) Object The key/value data sent to the server will be appended to the request URL as QueryString
callback(optional) Function The callback function is called when loading is successful (this method is only called when the return status of Response is success) and the request result and status are automatically passed to this method
type(optional) String The format of content returned by the server, including xml, html, script, json, text and _default

$.post() method.

The structure and usage of the $.post() and $.get() methods are the same, but there are still the following differences between them:

The GET request will pass the parameters after the URL, while the POST request will be sent to the web server as the entity content of the HTTP message.
The GET method has a size limit on the data transmitted (usually no more than 2KB), while the amount of data transmitted using the POST method is much larger than the GET method (theoretically not limited)
The data requested by GET method will be cached by the browser, so others can read the data from the browser's history, such as account number and password. In some cases, the GET method will cause serious security problems, while the POST method can relatively avoid these problems
The data transferred by GET method and POST method are obtained differently on the server side.

$.getScript(): jQuery provides this method to load js files directly, which is as simple and convenient as loading an HTML fragment, and does not require processing of JavaScript files, JavaScript files will be executed automatically.

The jQuery code is as follows:

Copy code The code is as follows:

$(function () {
           $("#send").click(function () {
               $.getScript("test.js");
        });
})

Like other ajax methods, the $.getScript() method also has a callback function, which will run after the JavaScript file is loaded successfully.

For example: If you want to load the jQuery official color animation plug-in (jquery.color.js), and bind the color change animation to the element after success:

Copy code The code is as follows:
















$.getJson(): This method is used to load JSON files, and its usage is the same as $.getScript().

Copy code The code is as follows:




   
   
   
   





   


已有评论:






test.json文件为:

复制代码 代码如下:

[
  {
    "username": "张三",
    "content": "沙发."
  },
  {
    "username": "李四",
    "content": "板凳."
  },
  {
    "username": "王五",
    "content": "地板."
  }
]

 
使用JSONP形式的回调函数来加载其他网站的JSON数据。例如:

复制代码 代码如下:




   
   
   
   



   







Note:

jQuery will automatically replace the callback function in the URL, such as the last "?" in "url?callback=?", with the correct function name to execute the callback function.
JSONP (JSON with Padding) is an unofficial protocol that allows integrating Script tags on the server side and returning them to the client, achieving cross-domain access through JavaScript Callback. Since JSON is just plain text with a simple bracket structure, many channels can exchange JSON messages. Due to the restrictions of the same origin policy, developers cannot use XMLHttpRequest when communicating with external servers. JSONP is a method that can bypass the same origin policy, that is, by using a combination of JSON and

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PUT request method in jQuery? How to use PUT request method in jQuery? Feb 28, 2024 pm 03:12 PM

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

PHP and Ajax: Building an autocomplete suggestion engine PHP and Ajax: Building an autocomplete suggestion engine Jun 02, 2024 pm 08:39 PM

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 get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

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:

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

How to implement PHP to jump to the page and carry POST data How to implement PHP to jump to the page and carry POST data Mar 22, 2024 am 10:42 AM

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

Use jQuery to modify the text content of all a tags Use jQuery to modify the text content of all a tags Feb 28, 2024 pm 05:42 PM

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on ​​the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

PHP code example: How to use POST to pass parameters and implement page jumps PHP code example: How to use POST to pass parameters and implement page jumps Mar 07, 2024 pm 01:45 PM

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

PHP vs. Ajax: Solutions for creating dynamically loaded content PHP vs. Ajax: Solutions for creating dynamically loaded content Jun 06, 2024 pm 01:12 PM

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.

See all articles