Home Web Front-end JS Tutorial A brief introduction to ajax

A brief introduction to ajax

Oct 10, 2017 am 10:15 AM
ajax introduce Simple

1.ajax native

ajax includes the following steps: 1. Create an AJAX object; 2. Issue an HTTP request; 3. Receive data returned by the server; 4. Update web page data. To summarize, in one sentence, ajax issues an HTTP request through the native XMLHttpRequest object, and then processes the data returned by the server.

Steps:

1

2

3

4

5

var xhr = createXHR();//创建对象

xhr.open(“方式”,”地址”,”标志位”);//初始化请求

xhr.setRequestHeader(“”,””);//设置http头信息

xhr.onreadystatechange =function(){}//指定回调函数

xhr.send();//发送请求

Copy after login

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

/   //1.创建xhr对象

    var xhr;

    if(window.XMLHttpRequest){

        xhr = new XMLHttpRequest();

    }else{

        xhr = new ActiveXObject('Microsoft.XMLHTTP');

    }

    //异步接受响应

    xhr.onreadystatechange = function(){

        if(xhr.readyState == 4){

            if(xhr.status == 200){

                //实际操作

              ;

            }

        }

    }

    //发送请求

    xhr.open('get',url,true);

    xhr.send();

Copy after login

2.jquery encapsulates ajax

1

2

3

4

5

6

7

8

9

10

11

$.ajax({

    type : "get",

    url : '',

    dataType : "json",

    data:{},

    success : function(data){

         

    },error:function(){

        console.log('fail');

    }

});

Copy after login

3.jsonp cross-domain principle

For security reasons , the browser prohibits ajaxcross-domain data acquisition

Step by step demonstration ofscriptsrc Attribute loadingjsFile method to obtain data

Explain the mechanism of cross-domain data acquisition

(1) Dynamic creation scriptTag

(2)Define callback function

(3) Return function call

(4) Pass parameters or global variables

The above is the detailed content of A brief introduction to ajax. 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 Article Tags

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)

The easiest way to query the hard drive serial number The easiest way to query the hard drive serial number Feb 26, 2024 pm 02:24 PM

The easiest way to query the hard drive serial number

How to solve the 403 error encountered by jQuery AJAX request How to solve the 403 error encountered by jQuery AJAX request Feb 20, 2024 am 10:07 AM

How to solve the 403 error encountered by jQuery AJAX request

How to solve jQuery AJAX request 403 error How to solve jQuery AJAX request 403 error Feb 19, 2024 pm 05:55 PM

How to solve jQuery AJAX request 403 error

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

How to get variables from PHP method using Ajax?

Introduction to the skills and attributes of Hua Yishan Heart of the Moon Lu Shu Introduction to the skills and attributes of Hua Yishan Heart of the Moon Lu Shu Mar 23, 2024 pm 05:30 PM

Introduction to the skills and attributes of Hua Yishan Heart of the Moon Lu Shu

PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions Feb 25, 2024 am 11:15 AM

PyCharm Beginner's Guide: Comprehensive Analysis of Replacement Functions

Detailed introduction of Samsung S24ai functions Detailed introduction of Samsung S24ai functions Jun 24, 2024 am 11:18 AM

Detailed introduction of Samsung S24ai functions

What is Dogecoin What is Dogecoin Apr 01, 2024 pm 04:46 PM

What is Dogecoin

See all articles