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

7
  • while echoeing from php use json_encode and in your javascript use JSON.parse of date then process it based on requirement. Commented Aug 31, 2015 at 6: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.. Commented Aug 31, 2015 at 6: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. Commented Aug 31, 2015 at 6:16
  • @SuchitKumar I think we have neared.. i am getting response like this.. but how can i get exactly like the first one... i.imgur.com/CzlLwqb.png Commented Aug 31, 2015 at 6:20
  • the first log is what you need it will be same as yours. Commented Aug 31, 2015 at 6:22

3 Answers 3

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]); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

THanks, i have updated what happens if i do json_encode and decode
In that way you will always get object from server-side.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.