JSON:响应没有结束

时间:2014-09-02 01:00:35

标签: php jquery json

我希望有人可以帮忙解决这个问题,我的表格是 signup.php ,其中ajax指向另一个页面 register.php 即可。我无法从 register.php 文件中调用的函数中获取json响应。我在register.php文件echo json_encode($register)中对函数的结果进行了编码,但在注册完成时没有显示任何内容。我在浏览器上设置调试器来跟踪网络响应,由于某种原因,标题内容类型是text / html,不确定是否存在问题。

我想要做的是成功显示该功能的响应,但它不起作用。这是我目前的代码。

signup.php

<script>
    $(document).ready(function()
    {
        $("#submit").click(function()
        {
            var formData = $("#signup").serializeArray();
            $.ajax(
                    {
                        type: "POST",
                        url: "../pages/register.php",
                        cache: false,
                        dataType: 'json',
                        data: formData,
                        success: function(response) {
                            $('#message').html(response);
                        }
                    });
            return false;
        });
    });
</script>

<div data-role="page" data-theme="a">
    <div data-role="header" data-position="inline">
        <h1>Carelincs</h1>
    </div>
    <div data-role="content" data-theme="a">
        <form action="" method="POST" id="signup">
            <label for="email">Email:</label>
            <input type="email" name="email" placeholder="eMail"/>
            <br />
            <label for="username">Username:</label>
            <input type="text" name="username" placeholder="User Name"/>
            <br />
            <label for="password">Password:</label>
            <input type="password" name="password" placeholder="Password"/>
            <br />
            <label for="confirm password">Confirm Password</label>
            <input type="password" name="repeatpassword" placeholder="Confirm Password"/>
            <br />
            <button type="submit" id="submit">Submit</button>
        </form>

        <p id="message"></p>
        <div data-role="popup" id="validate" class="">  
            <p>Account created! Activation email has been sent, check your email.</p>
        </div>
    </div>
</div>

register.php

<?php


require_once("../auth/config.class.php");
require_once("../auth/auth.class.php");

$config = new Config;

$dbh = new PDO("mysql:host=" . $config-> dbhost . ";dbname=" . $config->dbname,  $config->dbuser, $config->dbpass);


$auth = new Auth($dbh, $config);



$email = $_POST["email"];
$username = $_POST["username"];
$password = $_POST["password"];
$repeatpassword = $_POST["repeatpassword"];




$register = $auth->register($email, $username, $password, $repeatpassword ); 


// Temporary just so you can see what's going on :

echo json_encode($register); 
?>

1 个答案:

答案 0 :(得分:0)

如果要显示数据,则必须访问对象的字符串:

因此,根据您的情况,您可以将代码更改为div

<div id="message"></div>

然后解析该div中的json数据

 $('#message').html('<p> code: ' + response.code + '</p>');
 $('#message').append('<p> message: ' + response.message+ '</p>');