登录。onclick=function () { var xhr=new XMLHttpRequest (); xhr.open(“获得”、“http://localhost/ajax2/test2.php& # 63;用户名=" + username.value + "和pwd=" + pwd2.value, true); xhr.send (); xhr。onreadystatechange=function () { 如果(xhr。readyState==4) { 如果(xhr.status>=200,,xhr.status<300) { 警报(xhr.responseText); }; }; } }
btn。onclick=function () { ajax ( “得到”, “http://localhost/ajax2/my02.php”, {xingming: xingming.value pwd: pwd.value}, 函数(数据){ console.log(数据); }, 函数(errCode) { console.log (errCode); } )
01安全型. post更安全。
02速度。得到的速度快
03数量级。文章的数量级更大一些。
<强>具体实现:强>
var xhr=new XMLHttpRequest (); xhr.open(“后”,“http://localhost/ajax2/login2.php”,真正的); var=https://www.yisu.com/zixun/{数据 用户名:username1.value, pwd: pwd1.value }//设置请求头告诉服务器发给他的数据是json格式 xhr.setRequestHeader(“内容类型”、“application/json”); xhr。发送(JSON.stringify(数据)); xhr。onreadystatechange=function () { 如果(xhr。readyState==4) { 如果(xhr。状态在=200,,xhr。状态& lt;300){ 警报(xhr.responseText); }; }; }
var sc=document.createElement(“脚本”); sc.type=" text/javascript”; document.body.appendChild (sc); sc.src=" http://localhost/ajax2/jsonp.php& # 63; cb=myCallBack”; 函数myCallBack(数据){ console.log(数据); } >之前以上这篇基于js原生和ajax的get和post方法以及jsonp的原生写法实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
基于js原生和ajax的get和post方法以及jsonp的原生写法实例