2017-03-03 86 views
0

我正在为使用手机间隙的移动应用程序开发登录页面。但是,无论何时点击提交按钮,都会显示错误“无法POST”。为什么?请参阅下面的代码。单击提交按钮时无法开机自检/错误

的index.html

<body> 
    <div class="main-info2"> 
     <h3>Sign In</h3> 
      <div class="in-form"> 
       <form id="login_form" method="post""> 
        <input type="text" placeholder="Username" required=" " id="email" /> 
        <input type="password" placeholder="Password" required=" " id="password" /> 
        <div class="check-sub"> 
         <input type="submit" value="Login" id="login"> 
        </div> 
       </form> 
      </div> 
     </div> 
     <div class="copy-right"> 
      <p>Design by <a href="http://w3layouts.com">W3layouts</a></p> 
     </div> 
    </div> 
    <!-- //main --> 
</body> 

login.js

$(document).ready(function(){ 
    do_login(); 
}); 

function do_login() { 
    $("#login").click(function() { 
     var email = $('#email').val(); 
     var password = $('#password').val(); 
     var dataString="email="+email+"&password="+password+"&login="; 

     if($.trim(email).length>0 & $.trim(password).length>0){ 
      $.ajax({ 
       type: 'POST', 
       url: "http://localhost:1234/cleverpro/login.php", 
       dataType: 'json', 
       data: dataString, 
       crossDomain: true, 
       cache: false, 
       beforeSend: function(){ $("#login").html('Connecting...');}, 
       success: function(data){ 
        if(data == "success"){ 
         console.log("hay nako!"); 
         alert("hala"); 
        }else if(data == "failed"){ 
         $("#login").html('Login'); 
         console.log("hastang sayupa"); 
         alert("halajud"); 
        } 
       } 
      }); 
     }else{ 
      return false; 
      console.log("walay sulod uy"); 
     } 
    }); 
} 

的login.php

<?php 
include "db.php"; 

if(isset($_POST['login'])){ 
    $email=mysql_real_escape_string(htmlspecialchars(trim($_POST['email']))); 
    $password=mysql_real_escape_string(htmlspecialchars(trim($_POST['password']))); 
    $login=mysql_num_rows(mysql_query("select * from `user` where `email`='$email' and `password`='$password'")); 

    if($login!=0){ 
     echo "success"; 
    }else{ 
     echo "failed"; 
    } 
} 

?> 

我不知道在哪里做了我这是错误的。请帮帮我。

+0

你有一个尾随'“'后门柱,是一个错字? – Swellar

+0

你尝试把所有的代码在一个领域,看看它是否作品? –

+0

嗨。是的,这是一个错字。我已经删除它,但仍然是相同的错误 –

回答

1

首先,使用on('submit',handler)代替点击会更好。 (请参阅Whats the difference between onclick and onsubmit?

当您单击“登录”时,默认表单发送操作发生在您的JavaScript代码完成后。这意味着POST表单发生在你的表单的'行动'中,它没有在你的表单中设置,并且默认是'/'。

您需要的preventDefault行动,somethink这样

$('#login_form').on('submit', function(e) { 
    e.preventDefault(); 
    //Your code 
}); 
+0

你好。我试着添加这个,错误消失了,但没有任何反应,它没有提示。 –

+0

好吧,所以只需调试的东西,看看它什么时候跳出来。只需放置一个“console.log(i ++);”在每一行之后...并查看控制台中最后一个数字是什么。这应该会让你遇到问题线 –