ajax方法
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方法传参
它与个get方法的区别:
01 安全型。post更安全。
02 速度. get的速度快
03 数量级。 post的数量级更大一些.
具体实现:
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); }; }; }
原生jsonp 方法
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); }
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
以上就是原生ajax的get和post方法使用详解的详细内容。
