如何从ajax调用的php响应中制作数组的jquery对象 [英] How to make jquery objects of array from php response of ajax call

查看:104
本文介绍了如何从ajax调用的php响应中制作数组的jquery对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的JavaScript变量my,我想从php文件中获取它.

Here is my javascript variable my, i wanted to get it from php file.

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调用

As, i wanted to get it from php file. To achieve it I made a ajax call

    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);
        });

这样做的时候,我可以看到控制台的区别.

While doing this, I can see the difference in console..

这是我做console.log(my)console.log(data)

所以,我该如何做出正确的响应,该响应看起来像console.log(my)

So, how can i make the proper response which appears like inside the console.log(my)

注意:

var my是我需要的正确格式,我想从php文件中发布相同的格式,并在jquery中获取它.

The var my is the proper format that i need, I wanted to get the same format posted from php file and get it in 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成功后的响应...

//javascript response after success...

data=JSON.parse(data); 
for(var i=0;i<data.length;i++){ 
console.log(data[i]); 
}

这篇关于如何从ajax调用的php响应中制作数组的jquery对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆