Home > Article > Web Front-end > Detailed explanation of the use of native ajax get and post methods
login.onclick = function(){ var xhr = new XMLHttpRequest(); xhr.open("get","http://localhost/ajax2/test2.php?username="+username.value+"&pwd="+pwd2.value,true); xhr.send(); xhr.onreadystatechange = function(){ if (xhr.readyState == 4) { if (xhr.status>=200 && xhr.status<300) { alert(xhr.responseText); }; }; } }
ajax method
btn.onclick = function(){ ajax( "GET", "http://localhost/ajax2/my02.php", {xingming:xingming.value,pwd:pwd.value}, function(data){ console.log(data); }, function(errCode){ console.log(errCode); } )
Post method passing parameters
The difference between it and the get method:
01 Safety type. post is safer.
02 Speed. Get is fast
03 order of magnitude. The order of magnitude of post is larger.
Specific implementation:
var xhr = new XMLHttpRequest(); xhr.open("post","http://localhost/ajax2/login2.php",true); var data = { username:username1.value, pwd:pwd1.value } // 设置请求头 告诉服务器发给他的数据是json格式 xhr.setRequestHeader("content-type","application/json"); xhr.send( JSON.stringify(data) ); xhr.onreadystatechange = function(){ if (xhr.readyState == 4) { if ( xhr.status >= 200 && xhr.status < 300 ) { alert(xhr.responseText); }; }; }
Native jsonp method
var sc = document.createElement("script"); sc.type = "text/javascript"; document.body.appendChild(sc); sc.src = "http://localhost/ajax2/jsonp.php?cb=myCallBack"; function myCallBack(data){ console.log(data); }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
The above is the detailed content of Detailed explanation of the use of native ajax get and post methods. For more information, please follow other related articles on the PHP Chinese website!