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

A brief introduction to ajax

一个新手
Release: 2017-10-10 10:15:20
Original
1385 people have browsed it

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:

var xhr = createXHR();//创建对象 
xhr.open(“方式”,”地址”,”标志位”);//初始化请求 
xhr.setRequestHeader(“”,””);//设置http头信息 
xhr.onreadystatechange =function(){}//指定回调函数 
xhr.send();//发送请求
Copy after login
/   //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

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template