Home > Web Front-end > JS Tutorial > Analysis of AJAX usage examples in javascript_javascript skills

Analysis of AJAX usage examples in javascript_javascript skills

WBOY
Release: 2016-05-16 16:16:42
Original
1087 people have browsed it

The examples in this article describe the usage of AJAX in javascript. Share it with everyone for your reference. The specific analysis is as follows:

Compatibly obtain XMLHttpRequest object:

var xhr = null; 
if(window.XMLHttpRequest){ //非IE浏览器 
  xhr = window.XMLHttpRequest; 
}else if(window.ActiveXObject){ //IE浏览器 
  try{   //高版本,受msxml3.dll+支持 
    xhr = new ActiveXObject("Msxml2.XMLHTTP"); 
  }catch(e){ 
    try{  // 低版本,msxml2.6以下版本使用 
     xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
    }catch(e){ 
     alert("IE浏览器无法创建ActiveXObject对象!"); 
    } 
  } 
}
Copy after login

AJAX processing function:

xhr.open("POST",url,true); 
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
xhr.onreadystatechange=stateChangeHandler; 
xhr.send(); //var name="clf"; xhr.send(name); 
function stateChangeHandler(){ 
  if(xhr.readystate==4&&xhr.status==200){ 
   var obj = document.getElementById("targetDiv"); 
  obj.innerHTML = xhr.responseText; 
  } 
}
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

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