AJAX JSON + PHP JSON error


AJAX JSON + PHP JSON error

我有一个jQuery AJAX代码,它从php文件接收json类型的数据。没有dataType: "json"一切正常。但我需要json类型的数据。收到的文本是有效的JSON代码

{" ok ":"false","答案":"所有字段必须填写"}

但是当我使用dataType时,我得到了一个错误

对象"parsererror" SyntaxError

JS代码:

    $.ajax({
        url : "testing/regtest.php",
        type : "POST",
        dataType: "json",
        data : {
            mail : $('#mail_field').val(),
            username : $('#username_field').val(),
            password : $('#password_field').val(),
            password_2 : $('#rep_password_field').val()
        },
        success : function(data) {
            console.log(data.ok);
        },
        error: function(a,b,c) { console.log(a,b,c); }
    });

谢谢你的帮助!
更新:以下是php服务器端代码:http://jsfiddle.net/VfQbz/1/
更新2:它在IE9中工作,但在chrome中不工作

我不太确定这是否有效,但我认为问题是PHP糟糕的JSON编码函数。试一试:

if (check_post() === true) {
    $password = $_POST['password'];
    $password2 = $_POST['password_2'];
    $username = $_POST['username'];
    $mail = $_POST['mail'];
    if (valid_data($password, $password2, $username, $mail) === true){
        $answer = json_encode(array("ok" => "true", "answerswer" =>     $service_messages["account_registered"]));
        echo "'".$answer."'"; // Note the additional single quotes
    }
}

通过深入jQuery源代码,我发现了在响应解析中发生的错误。

你的PHP代码设置正确的标题吗?

header('Content-type: application/json');

只是为了涵盖所有的基础:你确定你的jQuery是最新的,没有以任何方式改变?