Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对象php和json编码_Javascript_Php_Json - Fatal编程技术网

Javascript 对象php和json编码

Javascript 对象php和json编码,javascript,php,json,Javascript,Php,Json,我的php服务器上有这样一个类: 问题是脚本没有向客户端发送jsonObject。我试图找到错误:脚本工作正常,因为我试图发回的不是jsonObject,而是我的类Voto的单个属性 这是我的另一个服务器语法 try{ $query="LOCK TABLES sostieneprova read;"; $success=mysqli_query($conn,$query); if($success){ $voto=getVoto($conn,$_POST['idP

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

问题是脚本没有向客户端发送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);
然后在一个exsternal文件中,我有一个带有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);
            });



        //}

}

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

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


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

如果代码没有引发异常,我希望您也解锁您的表。。。你有没有检查firebug是否有JSON消息,或者你是如何发现PHP没有发送JSON消息的?我忘了解锁表,但这不是问题所在。firebug没有错误;“allert”中的文本未定义。因为dettagliVoto对象不存在。我知道它只是关于UNLOCK语句的附加注释。但是你查过firebug的网络标签了吗?因为这听起来像是一个ajax api问题。我刚刚在我的第一篇博文中添加了一些代码。您的解决方案不起作用:我删除了json接口,但echo(json_encode($voto))不起作用。我已经通过firebug检查了响应,没有文本我解决了问题:我的文件是用cip252编码保存的,json不能正常工作!!!为了解决这个问题,我在php上找到了一个调用json_last_error_msg的函数,它向我显示了错误。
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);
            });



        //}