Home Web Front-end JS Tutorial jQuery simple Ajax call example

jQuery simple Ajax call example

Jun 30, 2017 pm 02:18 PM
ajax jquery Example

jQuery It’s really convenient. Let’s make a simple Ajax call:

Create a simple html file:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              //取Ajax返回结果
              //为了简单,这里简单地从文件中读取内容作为返回数据              htmlobj=$.ajax({url:"/Readme.txt",async:false});               //显示Ajax返回结果               $("#myp").html(htmlobj.responseText);
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>
Copy after login

Okay, click the button to see the effect.

Of course, jQuery's Ajax call can control many items, and a simple call is demonstrated here.

Pay attention to your own jqueryreferencepath.

Okay, let’s do an example of calling the background:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              
              //Ajax调用处理
            var html = $.ajax({
               type: "POST",
               url: "test.php",
               data: "name=garfield&age=18",
               async: false

            }).responseText;
            $("#myp").html('<h2>'+html+'</h2>');
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>
Copy after login

Backend code:


<?php    $msg='Hello,'.$_POST['name'].',your age is '.$_POST['age'].'!';    echo $msg;
Copy after login

Now you can get data from the background!

Of course, we can also call Ajax like this:


<!DOCTYPE HTML><html><head><script type="text/javascript" src="Public/js/jquery-easyui-1.3.1/jquery-1.8.2.min.js"></script><script type="text/javascript">
    $(function(){        //按钮单击时执行        $("#testAjax").click(function(){              
              //Ajax调用处理            $.ajax({
               type: "POST",
               url: "test.php",
               data: "name=garfield&age=18",
               success: function(data){
                        $("#myp").html('<h2>'+data+'</h2>');
                  }
            });
            
         });
    });</script>    </head>
    <body>
        <p id="myp"><h2>通过 AJAX 改变文本</h2></p>
        <button id="testAjax" type="button">Ajax改变内容</button>
    </body></html>
Copy after login

Note that the data parameter in

success: function(data)

can be changed to another name, such as success: function( msg), msg(data) is the returned data. This is the parameter of the callback function, not the data parameter in the Ajax call in

data: "name=garfield&age=18"

.

The above is the detailed content of jQuery simple Ajax call example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Go language indentation specifications and examples Go language indentation specifications and examples Mar 22, 2024 pm 09:33 PM

Indentation specifications and examples of Go language Go language is a programming language developed by Google. It is known for its concise and clear syntax, in which indentation specifications play a crucial role in the readability and beauty of the code. effect. This article will introduce the indentation specifications of the Go language and explain in detail through specific code examples. Indentation specifications In the Go language, tabs are used for indentation instead of spaces. Each level of indentation is one tab, usually set to a width of 4 spaces. Such specifications unify the coding style and enable teams to work together to compile

Oracle DECODE function detailed explanation and usage examples Oracle DECODE function detailed explanation and usage examples Mar 08, 2024 pm 03:51 PM

The DECODE function in Oracle is a conditional expression that is often used to return different results based on different conditions in query statements. This article will introduce the syntax, usage and sample code of the DECODE function in detail. 1. DECODE function syntax DECODE(expr,search1,result1[,search2,result2,...,default]) expr: the expression or field to be compared. search1,

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

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 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.

Application and example analysis of PHP dot operator Application and example analysis of PHP dot operator Mar 28, 2024 pm 12:06 PM

Application and example analysis of PHP dot operator In PHP, the dot operator (".") is an operator used to connect two strings. It is very commonly used and very flexible when concatenating strings. By using the dot operator, we can easily concatenate multiple strings to form a new string. The following will introduce the use of PHP dot operators through example analysis. 1. Basic usage First, let’s look at a basic usage example. Suppose there are two variables $str1 and $str2, which store two words respectively.

Example of Curl Get command Example of Curl Get command Mar 20, 2024 pm 06:56 PM

In Linux, URL or Curl client is a popular command line utility that allows you to transfer data over the network using various protocols such as HTTPS, HTTP, FTP, etc. It allows you to send and receive data using its get, post and request methods. Among them, you need to use the &quot;get&quot; method frequently. Therefore, it becomes crucial to learn various methods and various options that you can use to increase your productivity. &quot;Performing a curl operation is as simple as entering a few simple commands. Although it seems simple, many users do not fully realize its potential. Therefore, this short guide provides some information on how to perform curl operations on Linux systems. Example using the &quot;curlget&quot; command.&quot; Curl

See all articles