try this: $data = array('votes_for'=>7, 'votes_against'=>4); echo json_encode($data); exit;
On Oct 19, 5:31 am, Maarten <maartenwie...@gmail.com> wrote: > Hi, > > I am using jQuery.get to make an AJAX request to the server which > should return a JSON object, but it is interpretated by jQuery as a > string, not an object. > > Here is the javascript code I use: > > $.get(this.href, function(data) { > console.log(data); > console.log(data.votes_for); > console.log(typeof data); > > }, "json"); > > And this is the PHP code on the server side. > > echo header('Content-type: application/x-json'); > echo '{"votes_for":7,"votes_against":4}'; > die(); > > I do not use getJSON because I want to make one request which could > return in a JSON object, or just a string, depending on some > conditions. > > Can anyone help me out? Thanks in advance! > > Regards, > Maarten