How to make jquery array objects from php response of ajax call

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

Inside selectAnnotation.phpI just repeat it

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;

Like, I wanted to get it from a php file. For this, I made an 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);
        });

However, I see a difference in the console.

Here is a screen of what happens, I am making a console log console.log(my)andconsole.log(data)

enter image description here

So how can I make the correct answer that appears both inside console.log(my)

Note:

var my is the correct format that I need, I wanted to get the same format that was sent from the php file and get it in jquery.

Update:

When i try to do

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

and in jquery

function(data,status){
        var obj = jQuery.parseJSON( data);
        console.log(obj);
        });

I get almost the same as the previous one.

0
source share
3

:

$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]); 
}
+1

json_encode json_decode . js JSON.parse

+1

His is my first answer to SO.

I assume the correct way to do this is to use eval (). However, it is recommended to use json_encode and json_decode, as suggested by Ninjava.

0
source