2017-03-03 22 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 ++);」在每一行之後...並查看控制檯中最後一個數字是什麼。這應該會讓你遇到問題線 –