<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 Failed to load http://localhost/graduationProject/server/login.php: Redirect from 'http://localhost/graduationProject/server/login.php' to 'http://localhost/graduationProject/browser/flappybirds/index.html' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
Cross-domain problem, php adds header('Access-Control-Allow-Origin:*');