wordpress xmlrpcseo崗位有哪些
//FormData對象是XMLHTTPRequest level2新增的類型,它可以自動序列化表單內(nèi)容,不再需要我們?nèi)懶蛄谢韱畏椒?#xff1b;
FormData()即可以直接把整個表單給它,也可以分別使用append(‘字段’,‘值’)方法給FormData();
現(xiàn)在就結(jié)合xhr寫一個簡單示例,怎么把表單傳遞給后端
<html><head><meta http-equiv="content-type" content="text/html;charset=UTF-8"/><title>測試xhr和formdata</title></head><body><div id="regform"><form id="regniter" name="reg" method="post" action=""><label><div class="inputStyle">用戶名:<input type="text" name="username" value=""/></div></label><label><div class="inputStyle">密 碼:<input type="password" name="pwd" value=""/></div> </label><label><div class="inputStyle"><input type="button" name="submit1" value="提交"/></div></label></form></div><script>let btn=document.getElementsByName("submit1");btn[0].addEventListener("click",function(){let xhr=new XMLHttpRequest();xhr.onreadystatechange=function(){if(xhr.readyState===4){try{if((xhr.status>=200&& xhr.status<300) || xhr.status===304){//獲取從后端返回的數(shù)據(jù)let dataarr=JSON.parse(xhr.response);console.log(dataarr);}else{console.log("連接后端不成功:",xhr.status);}}catch(ex){xhr.ontimeout=function(){alert('連接超時');};}}};xhr.open("post","regniter.php");let form1=document.forms[0];let username=form1.elements['username'].value;let pwd=form1.elements['pwd'].value;//設(shè)置超時為5秒xhr.timeout=5000;//設(shè)置重寫xhr響應(yīng)的mime類型,這里是json類型,也可以寫成application/json形式xhr.overrideMimeType("text/json");if(username!=="" && pwd!==""){let data=new FormData(form1);xhr.send(data);/*這里是單獨(dú)設(shè)置字段/值,效果和上面是一樣的data.append('username')=username;data.append('pwd)=pwd;xhr.send(data);*/form1.elements['submit1'].disabled=true;console.log('已經(jīng)發(fā)送');}})</script></body>
</html>
//這里是后端處理
if(isset($_POST['username'])&&isset($_POST['pwd']))
{$regArr=[];$regArr['username']=$_POST['username'];$regArr['pwd']=$_POST['pwd'];//注意:這里不能用return,即使是文本也不能用return,不然前端收到是空白 echo json_encode($regArr);
}else
{echo 'no';
}