The Old Way & The NEW WAY IN AJAX
My Code: AJAX AND GET
AJAX AND POST
var xmlhttp; xmlhttp = GetXmlHttpObject(); if(xmlhttp == null){ alert("Boo! Your browser doesn't support AJAX!"); return; } xmlhttp.onreadystatechange = stateChanged; xmlhttp.open("GET", "http://www.google.com", true); xmlhttp.send(null); function stateChanged(){ if(xmlhttp.readyState == 4){ // do something with the response text alert(xmlhttp.responseText); } } function GetXmlHttpObject(){ // IE7+, Firefox, Chrome, Opera, Safari if(window.XMLHttpRequest){ return new XMLHttpRequest(); } //IE5, IE6 if(window.ActiveXObject){ return new ActiveXObject("Microsoft.XMLHTTP"); } return null; }
AJAX AND POST
var xhr = new XMLHttpRequest(); xhr.open('POST', 'somewhere', true); xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); xhr.onload = function () { // do something to response console.log(this.responseText); }; xhr.send('user=person&pwd=password&organization=place&requiredkey=key'); Or if you can count on browser support you could use FormData: var data = new FormData(); data.append('user', 'person'); data.append('pwd', 'password'); data.append('organization', 'place'); data.append('requiredkey', 'key'); var xhr = new XMLHttpRequest(); xhr.open('POST', 'somewhere', true); xhr.onload = function () { // do something to response console.log(this.responseText); }; xhr.send(data);NEW AJAX
Untitled Document
- Loading Data..
Nhận xét
Đăng nhận xét