我的 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);
});
//}
}