您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

原生ajax的get和post方法使用详解

2024/3/26 9:13:15发布14次查看
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方法
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方法使用详解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product