这是我的 javascript 变量 my
,我想从 php 文件中获取它。
在selectAnnotation.php
中我只是回显它
echo "{
src : 'http://192.168.1.58/annotate/drive/image/test.jpg',
text : 'Suresh and Gopinath....',
shapes : [{
type : 'rect',
geometry : { x : 0.1825726141078838, y: 0.23756906077348067, width : 0.11602209944751381, height: 0.11618257261410789 }
}]
}";
exit;
因为,我想从 php 文件中获取它。为了实现它,我进行了 ajax 调用
var my = {
src : 'http://192.168.1.58/annotate/drive/image/<?php echo $_GET['file']?>',
text : 'Suresh and Gopinath....',
shapes : [{
type : 'rect',
geometry : { x : 0.1825726141078838, y: 0.23756906077348067, width : 0.11602209944751381, height: 0.11618257261410789 }
}]
}
console.log(my);
console.log('__________');
$.post("masterCall/selectAnnotation.php",
{ url: 'http://192.168.1.58/annotate/drive/image/<?php echo $_GET['file']?>'
},
function(data,status){
console.log(data);
console.log('__________');
//anno.addAnnotation(data);
});
在执行此操作时,我可以看到控制台中的差异..
这是我对 console.log(my)
和 console.log(data)
进行控制台日志时发生的情况的屏幕
那么,我怎样才能做出正确的响应,就像在 console.log(my)
注意:
var my
是我需要的正确格式,我想从 php 文件获取相同的格式并在 jquery 中获取它。
更新:
当我尝试做
$v = "{
src : 'http://192.168.1.58/annotate/drive/image/test.jpg',
text : 'Suresh and Gopinath....',
shapes : [{
type : 'rect',
geometry : { x : 0.1825726141078838, y: 0.23756906077348067, width : 0.11602209944751381, height: 0.11618257261410789 }
}]
}";
echo json_encode($v);
和 jquery
function(data,status){
var obj = jQuery.parseJSON( data);
console.log(obj);
});
我得到的几乎和之前的一样..
最佳答案
尝试这样构建:
$Result = $Connection->query($Query);
$result=array();
$i=0;
while($row = $Result->fetch_assoc()){
$result[$i]['src']=$row['src'];
$result[$i]['text']=$row['text'];
$result[$i]['shapes'][]=array('type'=>'rect','geometry'=>array('x' => $row['x'], 'y'=> $row['y'], 'width' => $row['width'], 'height'=>$row['height']) );
$i++;
}
echo json_encode($result);
//成功后的javascript响应...
data=JSON.parse(data);
for(var i=0;i<data.length;i++){
console.log(data[i]);
}
关于javascript - 如何从ajax调用的php响应中创建数组的jquery对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32304884/