0

我的 php 服务器上有这个类:

class Voto implements JsonSerializable {
    public $voto;
    public $info;
    public $codiceFiscaleStudente;
    public $data;
    public $idProva;


function __construct($voto,$idProva,$codiceFiscaleStudente,$data) {

    $this->codiceFiscaleStudente=$codiceFiscaleStudente;
    $this->voto=$voto;
    $this->idProva=$idProva;
    $this->data=$data;
    $this->info=null;
}

public function jsonSerialize() {
    return get_object_vars($this);
}

}

问题是脚本没有将 jsonObject 发送给客户端。我试图找到错误:脚本工作正常,因为我试图发回的不是 jsonObject,而是我的类 Voto 的单个属性。

这是我的其他服务器语法

try{
$query="LOCK TABLES sostieneprova read;";
$success=mysqli_query($conn,$query);
if($success){
                $voto=getVoto($conn,$_POST['idProva'],$_POST['studente_codiceFiscale']);
if(gettype($voto)==="object"){
$voto->info="Esiste già un voto dello studente in data ".$voto->data;
/*if in my Ajax-request i set datatype="html" and i send back echo($voto->info) it works fine.Instead if in my AJAX CALL in javascript i have set datatype "json" javascript doesn't receveid a jsonObject but an empty string.i think that the problem is with the function below json_encode*/
echo(json_encode($voto));
}
else{
throw new Exception ("Non esiste alcun voto dello studente nella prova: ".$_POST['idProva']);
//$query="UNLOCK TABLES;";
//mysqli_query($conn,$query);

}
}
else{
     echo("Impossibile bloccare le tabelle per caricare il voto");
        }
        }
            catch(Exception $e){
                $query="UNLOCK TABLES;";
                mysqli_query($conn,$query);
                echo($e->getMessage());
            }

JAVASCRIPT:

var ajax=$.ajax({
            url:"responseregistrodocente.php",
            data: queryString,
            type:"POST",
            datatype:"json",
            cache:false,
            ifModified:false});

            ajax.done(
            function(dettagliVoto){
            alert(dettagliVoto.codiceFiscale);
            });

            ajax.fail(function(jqxhr){
            alert(jqxhr.responseText);
            });

感谢帮助。

如果这有帮助:在我的 html 页面中,我有一个 jquery document.ready,里面有:

$(".submitProve").on("click","button[name='visualizzaVoto']",caricaVotoHandler);

然后在一个外部文件中我有ajax请求的功能

function caricaVotoHandler(evento){
    evento.stopPropagation();
    //here there is other program code to build the queryString and the querystring //send the data correctly to the server.I've verified
            var ajax=$.ajax({
            url:"responseregistrodocente.php",
            data: queryString,
            type:"POST",
            datatype:"json",
            cache:false,
            ifModified:false});

            ajax.done(
            function(dettagliVoto){
            alert(dettagliVoto);
            });

            ajax.fail(function(jqxhr){
            alert(jqxhr.responseText);
            });



        //}

}

4

1 回答 1

0

如果您只更改echo json_encode($voto);为脚本输出正确,echo $voto->codiceFiscaleStudente;那么您的问题在于您的JsonSerializable接口实现。尝试不使用它:

如果您的类Voto仅包含公共成员,所有这些都将包含在 JSON 序列化中,则无需实现JsonSerializable. 调用json_encode()您的对象$voto将返回正确的 JSON 字符串。

此外,在您的第一个 JS 片段中,您的警报调用alert(dettagliVoto.codiceFiscale);看起来应该dettagliVoto.codiceFiscaleStudente改为使用它来匹配您的Voto类的该属性。

于 2013-10-20T09:42:29.803 回答