<form id="loginForm" method="post" onsubmit="return false;"> <div> <input type="text" name="userid" id="userid" class="userid" placeholder="id" autocomplete="off" onfocus="" onblur="" /><!-- autocomplete="off" 关闭自动补全 --> </div> <div> <input type="text" name="username" id="username" class="username" placeholder="用户名" autocomplete="off" onfocus="" onblur="" /> </div> <div> <input type="password" name="password" id="password" class="password" placeholder="密码" oncontextmenu="return false" onpaste="return false" /> <!-- oncontextmenu="return false"关闭右键打开菜单功能 onpaste="return false"关闭粘贴功能 --> </div> <button id="submit" type="submit" onclick="mysubmit();">登 录</button> </form>
表單結構
function mysubmit(){ var xhr ; if (window.XMLHttpRequest) { // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码 xhr=new XMLHttpRequest(); } else { // IE6, IE5 浏览器执行代码 xhr=new ActiveXObject("Microsoft.XMLHTTP"); } xhr.open("POST","http://localhost/graduationProject/server/login.php",true); xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); xhr.send("userid=1111&username=1111&password=1111"); // body... }
#ajax請求
#瀏覽器報錯:index.html:1 無法載入http://localhost/graduationProject/ server/login.php:從「http://localhost/graduationProject/server/login.php」重新導向至「http://localhost/graduationProject/browser/flappybirds/index.html」已被CORS 策略阻止:否「 Access-Control-Allow-Origin'標頭存在於所要求的資源上。因此不允許存取原點“null”。
跨域問題, php加入 header('Access-Control-Allow-Origin:*');