Javascript 如何使用ajax和json登录hibrid移动应用程序

Javascript 如何使用ajax和json登录hibrid移动应用程序,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我是一个新手程序员。对于我的项目,我正在尝试使用json和ajax为移动web创建一个登录表单。我试着自己测试教程中的代码 这是我的代码: <!DOCTYPE html> <html> <head> <title>JQM Demo Login</title> <meta name="viewport" content="width=device-width, height=device-height, initia

我是一个新手程序员。对于我的项目,我正在尝试使用
json
ajax
为移动web创建一个登录表单。我试着自己测试教程中的代码

这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>JQM Demo Login</title>
    <meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0" />
    <link rel="stylesheet" type="text/css" href="libs/jquery.mobile-1.4.3.min.css" />
    <script type="text/javascript" src="libs/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="libs/jquery.mobile-1.4.3.min.js"></script>
    <script type="text/javascript" src="js/login.js"></script>
</head>
<body>
    <div data-role="page" id="login">
        <div data-role="header">
            <h3>Login Page</h3>
        </div>

        <div data-role="content">
            <form id="check-user" class="ui-body ui-body-a ui-corner-all" data-ajax="false">
                <fieldset>
                    <div data-role="fieldcontain">
                        <label for="username">Username</label>
                        <input type="text" value="" name="username" id="username" class="ui-theme-b" />
                    </div>
                    <div data-role="fieldcontain">
                        <label for="username">Password</label>
                        <input type="password" value="" name="password" id="password" class="ui-theme-b" />
                    </div>
                    <input type="button" data-theme="b" name="submit" id="submit" value="Login">
                </fieldset>
            </form>
        </div>

        <div data-role="footer" data-position="fixed">

        </div>
    </div>

    <div data-role="page" id="second">
        <div data-role="header">
            <h3>Welcome Page</h3>
        </div>

        <div data-role="content">

        </div>
    </div>
</body>
</html>
它使用ajax将数据定向到localhost中同一服务器中的check.php

<?php
    $action = $_POST['action'];

    parse_str($_POST['formData'], $output);

    $username = $output['username'];
    $password = $output['password'];

    $output = array('status' => true, 'message' => $username);
    echo json_encode($output);  

?>

在教程“”提供的链接中,代码工作正常。但对我来说,当我尝试实现同样的方法时,我的错误率正在下降

发生网络错误,请重试


谁能告诉我我的代码有什么问题吗?如果用户名和密码通过了MySQL数据库的身份验证,那么如何使其成功呢?

您的JS代码看起来不错,我建议您显示“错误”以进行调试。在error方法中尝试console.log(error)

无论如何,可能的原因可能是:

  • check.php返回的数据不是json格式。因此,如果您返回“YES”或“1”等,请确保它是JSON格式
  • SQL查询中存在一些异常/错误。因此,您将获得自定义消息

  • 我现在把它修好了。但是,第二页的结果是“欢迎未定义”。我认为用户名中不包含result user.name。有人能告诉我任何问题的细节以及如何解决吗?那么,有没有一些方法可以使返回内容成为json格式?顺便说一句,它还没有与数据库sql同步。
    <?php
        $action = $_POST['action'];
    
        parse_str($_POST['formData'], $output);
    
        $username = $output['username'];
        $password = $output['password'];
    
        $output = array('status' => true, 'message' => $username);
        echo json_encode($output);  
    
    ?>