<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:*');