0

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

Inside the selectAnnotation.php i am just echoing 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;

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..

Here is the screen of what happens i do console log of console.log(my) and console.log(data)

enter image description here

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

Note :

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.

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 am getting almost same like previous one..

SA__
  • 1,782
  • 3
  • 20
  • 41
  • while echoeing from php use `json_encode` and in your javascript use `JSON.parse` of date then process it based on requirement. – Suchit kumar Aug 31 '15 at 06:13
  • @SuchitKumar when i do `json_encode` in php and `var obj = JSON.parse( data);` in jquery i am getting the same result like previous one.. – SA__ Aug 31 '15 at 06:15
  • use `multidimensional array` to construct data in php not string and log it without parsing i.e. before `JSON.parse( data);` and see the difference. – Suchit kumar Aug 31 '15 at 06:16
  • @SuchitKumar I think we have neared.. i am getting response like this.. but how can i get exactly like the first one... http://i.imgur.com/CzlLwqb.png – SA__ Aug 31 '15 at 06:20
  • the first log is what you need it will be same as yours. – Suchit kumar Aug 31 '15 at 06:22
  • @SuchitKumar here is my eval.. https://eval.in/425020 first log is the one i did from jquery and second log is the one i did from http response.. – SA__ Aug 31 '15 at 06:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88340/discussion-between-anto-and-suchit-kumar). – SA__ Aug 31 '15 at 06:23

3 Answers3

1

try to use json_encode and json_decode to transfer your data. In js JSON.parse

0xdenishdev
  • 186
  • 4
  • 10
1

Try constructing like this:

$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 response after success...

data=JSON.parse(data); 
for(var i=0;i<data.length;i++){ 
console.log(data[i]); 
}
Suchit kumar
  • 11,809
  • 3
  • 22
  • 44
0

Its my first answer on SO.

I guess the correct way to do it would be to use eval(). However it is rather recommended to use json_encode and json_decode as Ninjava has suggested.