javascript - 如何从ajax调用的php响应中创建数组的jquery对象

标签 javascript php jquery

这是我的 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) 进行控制台日志时发生的情况的屏幕

enter image description here

那么,我怎样才能做出正确的响应,就像在 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/

相关文章:

javascript - UploadCare 不在对话框中加载图像

javascript - 使用 jQuery 修复 html 表的部分

php - AS3动态数据加载不正确

php - onfocus如何操作?

php - 使用 fetchcolumn() while 循环在 pdo 中无法正常工作后

jquery - 伪装成不需要的亲戚的超链接

javascript - 在 html 页面中编写所有 javascript 是一个好习惯吗?

javascript - Web 用户控件的 getElementByID() 不起作用?

javascript - onclick 打开子窗口并提交表单

javascript - CSS背景图片突然消失