Summary of knowledge points of jQuery's AJAX
This time the editor has compiled the knowledge points of AJAX in jQuery for everyone. The following is a summary of the knowledge points. Let’s take a look.
1. AJAX concept
Asynchronous Javascript And XML(Asynchronous JavaScript and XML)
AJAX is not a language, but a way to create interactive web applications Web development technology
AJAX is a combination of Javascript, XHTML and CSS, DOM, XML and XSTL, XMLHttpRequest and other technologies
1. Use XHTML+CSS to standardize presentation;
2. Use XML and XSLT for data Exchange and related operations;
3. Use the XMLHttpRequest object for asynchronous data communication with the Web server;
4. Use Javascript to operate the Document Object Model (network document object model) for dynamic display and interaction;
5.Use JavaScript binds and processes all data
What is XML?
XML refers to Extensible Markup Language (EXtensible Markup Language)
XML is a markup language, very similar to HTML
XML is designed to transmit Data, not display data
The XML tag is not predefined. You need to define the labels yourself.
XML is designed to be self-describing.
XML is a recommended standard by W3C
What is XSLT?
XSLT refers to XSL Transformations [1]
XSLT is the most important part of XSL
XSLT can convert one type of XML document into another type of XML document
XSLT uses XPath in XML Navigation in the document
##User operation process:
User browser->JavaScript instantiation XmlHttpRequest object->AJAX engine->http request->web server->backend business system
System return process :
Backend business system->Backend server->web server->HTML, XML, JSON data->AJAX engine->HTML+CSS (Wel browser)->User browser
##3. AJAX advantages and disadvantages:
AJAX asynchronous processing advantages:
Reduce the burden on the server, AJAX generally only obtains only what is needed from the server Data
No refresh page update, reducing user waiting time
For better customer experience, some server work can be transferred to the client to complete, saving network resources and improving user experience
No platform restrictions
Promote the separation of display and data
Disadvantages of AJAX asynchronous processing:There is a large amount of JS in the page, which brings difficulties to search enginesAJAX kills the Back and History functions, that is, it destroys the browser mechanism There is a cross-domain problemOnly utf-8 encoded data can be transmitted and received
1.AJAX implementation steps
window.open(URL,name,features,replace)
URL:
An optional string that declares the URL of the document to be displayed in the new window. If this parameter
is omitted or its value is an empty string, then the new window will not display any documents name:An optional string that is a A comma-separated list of characters, including numbers, letters, and underscores, This character declares the name of the new window. This name can be used as the value of the target attribute of the tags and
if (window.XMLHttpRequest) {// Mozilla, Safari, ... var http_request = new XMLHttpRequest(); } else if (window.ActiveXObject) { // IE 5 ,6 var http_request = new ActiveXObject("Microsoft.XMLHTTP"); }
XMLHttpRequest issues an HTTP request
http_request.open("GET|POST","test.php?GET方式传值",true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //仅POST请求时需要设置 //用于向一个已经连接的socket发送数据 //如果是POST传输方式要把值写在send()函数里 http_request.send(向一个已连接的套接口发送数据); XMLHttpRequest取得响应数据并显示 http_request.onreadystatechange=function(){ if(http_request.readyState==4 && http_request.status==200){ $("p").text(http_request.responseText) } }
Example:
//GET method
Parameter 1: Represents whether to send the request in get or post mode Parameter 2: To whom to send the request url Parameter 3: true represents an asynchronous request, false represents a synchronous request
http_request.open("GET","test.php?user_name="+username.val(),true); http_request.send();
Send POST request
var username=$("input[name='user_name']");
Parameter 1: Represents whether to send the request in get or post mode Parameter 2 : Which url to send the request to Parameter 3: true represents an asynchronous request, false represents a synchronous request
http_request.open("POST","test.php",true); http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
If it is a POST transmission method, the value must be written in the send() function
http_request.send({username:username});
4.JSON
Javascript Object Notation is a lightweight data exchange format
Every language knows JSON, so use it for various languages Data conversion
JSON supports multiple languages
Format
{key:value,key:value,.....} Object format
[{key:value,key:value,.. ...},{key:value,key:value,.....},...] Array format
PHP processing:
$json=json_encode($array) //Yes Variables are Json encoded
$array=json_decode($json) //Decode Json data and convert it into PHP variables
JavaScript processing:
eval('('+json+')') //Convert a certain A string is executed according to JS code
Example:
eval("x=10;y=20;document.write(x*y)") document.write(eval("2+2")) JSON.parse(json) //对传过来的json字符串进行解码,变成JS认识的对象 JSON.stringify(obj) //将JS中的值编译成json字符串
5. AJAX application in jQuery 1
. Don’t forget to write
$.ajax({ //你要传的php文件的路径 url:"test1.php", ('服务器url地址') //以get方式传输拼接字符串 data:"user_name="+$('input[name="user_name"]').val(),('名=值&名=值&.....',) //以什么方式传输 type:'get',('post|get') //传输返回的数据类型 dataType:'json', ('xml|html|text|json|script') //展示 数据的方式 success:function(res){ $('h1').text('用户名为:'+res.user_name); }, //错误信息 error:function(xhr){ }, timeout:2000, async:true, cache:false })
6. AJAX application in jQuery 2
$.get() $.get('服务器url地址',"json格式或字符串格式",function(res){ //处理返回的数据 }), "xml|html|json|text|script")
Example:
//'服务器url地址',"json格式或字符串格式" $.get("test1.php",{user_name:$("input[name='user_name']").val()},function(data){ //如果后台发过来的值跟这里的值相等让他执行下面代码 if(data.status=='ok'){ alert("登陆成功"); location.href="http://www.wl.com"; }else{ alert("登陆失败"); } //"xml|html|json|text|script"类型 },'json')
7. AJAX application in jQuery 3
serialize( ) The content of the sequence list table is a string, and the serialized data is used for Ajax requests
$.post() $.post('服务器url地址',"json格式或字符串格式",function(res){ //处理返回的数据 }), "xml|html|json|text|script")
Example:
//'服务器url地址',"json格式或字符串格式" 用post方式提交要用form表单包起来 // 然后用serialize()来拿里面所有有值 $.post("test1.php",$('form').serialize(),function(res){ //如果后台发过来的值跟这里的值相等让他执行下面代码 if(res.status=='ok'){ alert("登陆成功"); location.href="http://www.wl.com"; }else{ alert("登陆失败"); } //"xml|html|json|text|script"类型 },'json')
Receive and process output in php
try{ $pdo=new PDO("mysql:host=127.0.0.1;port=3306;dbname=数据库名",'数据库账号','数据库密码'); $pdo->exec("set names utf8"); $sen=$pdo->query("select * from yh_admin where user_name='{$user_name}' limit 1"); if($sen->rowCount()>0){ // $arr=$sen->fetch(PDO::FETCH_ASSOC); //echo json_encode($arr); //echo 'yes'; $arr['status']='ok'; }else{ echo 'no'; } //切记用json数据类型传输 echo json_encode($arr); }catch (PDOException $e){ echo $e->getMessage(); }
The above is the detailed content of Summary of knowledge points of jQuery's AJAX. 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

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

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:

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

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:

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.

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
