简体   繁体   中英

unable to get json data from server

My server side code is:

$bus = array(
       'meaning' => $name
       );

$jsonstring = json_encode($bus);
echo $_GET['callback'].'(' . $jsonstring. ')';

the value displayed on the screen is correct - ?word=heart({"meaning":"heart"}) but when I am reading it with following code its printing the value of meaning as 11200665987893779229_1460521505942

$(document).ready(function(){
    $.getJSON('http://mydomain?callback=?','word=heart',function(res){
     document.getElementById('print').innerText=''+res.meaning;
    });
});

but when I do this:

$bus = array(
       'meaning' => 'heart'
       );

it's printing the correct value ie heart

I am not getting why this is happening and how to get the correct value (I am accessing data from my different domain) .

JSON.parse() converts any JSON String passed into the function, to a JSON Object.

$(document).ready(function(){
$.getJSON('http://mydomain?callback=?','word=heart',function(res){
obj = JSON.parse(res);
 document.getElementById('print').innerText=''+obj.meaning;
});

});

a similar post is here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM