【问题标题】:JSON: Response not coming overJSON:响应没有过来
【发布时间】:2014-09-02 01:00:35
【问题描述】:

我希望有人可以帮助解决这个问题,我有一个带有 ajax 的表单 signup.php 可以发布到另一个页面 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); 
?>

【问题讨论】:

  • response 包含什么,你做过console.log(response); 吗?如果它是一个对象,你不能像.html()那样使用它。
  • 在输出 JSON 之前尝试 header('Content-Type: application/json'),因为 AJAX 方法将寻找它,因为您明确表示您期望它:dataType: 'json',
  • 还要检查 AJAX 调用中的“错误”。如果您有一些无法翻译为 JSON 的内容,jQuery 会错误地调用该函数。 console.log() 那个也是。
  • @cilosis 明确定义数据类型意味着服务器确实不必发送内容类型标头。
  • @jeroen 这是响应包含的内容,它显示在响应正文 {"code":200,"message":"register_success"}

标签: php jquery json


【解决方案1】:

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

所以对于您的情况,您可以将标签更改为 div

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

然后解析那个div中的json数据

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2019-10-28
    • 1970-01-01
    • 2014-04-10
    • 2010-11-11
    相关资源
    最近更新 更多