简体   繁体   English

单击提交按钮时无法发布/错误

[英]Cannot POST / error when clicking Submit button

I am developing a login page for our mobile app using Phone gap. 我正在使用Phone gap为我们的移动应用程序开发一个登录页面。 But whenever I clicked the Submit button, an error 'Cannot POST /' appears. 但是,每当我单击“提交”按钮时,都会出现错误“无法发布/”。 Why is that so? 为什么会这样? Please see my code below. 请在下面查看我的代码。

index.html 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 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 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";
    }
}

?>

I didn't know where did I gone wrong with this. 我不知道我在哪里出错了。 Please help me. 请帮我。

First of all it would be better to use on('submit', handler) instead on click. 首先,最好在单击时使用on('submit',handler)。 (see Whats the difference between onclick and onsubmit? ) (请参阅onclick和onsubmit有什么区别?

When you clicks "Login", default form send action happens after your javascript code finishes. 当您单击“登录”时,默认的表单发送操作将在您的JavaScript代码完成后发生。 It means that form POST happens to 'action' of your form that is not set in your form and is '/' by default. 这意味着表单POST发生在表单的“操作”中,表单的“操作”未在表单中设置,默认情况下为“ /”。

You need to preventDefault action, somethink like this 您需要阻止Default操作,像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM