2

Im receiving a text from a php script over ajax using jquery like this:

$.ajax({
        dataType: 'json',
           url: 'someURL.com',
                success:function(result){
                    json = JSON.parse(result.rgraph);
})

The result.rgraph is sended in php as:

$arr = array( 'actiondata'=> 'some info here',  'rgraph' => $this->_rgraph() );
$jsonTable = json_encode($arr);

return $jsonTable;

Where $this->_rgraph() is just a function that returns:

return '{ id: "0_0", name: "Categorie", children: [{ id: "0_1", name: "Business services", data: { relation: "" },children: [{ id: "0_2", name: "H-FARM Ventures", data: { relation: "" }, children: [] }]},{ id: "0_3", name: "Cause", data: { relation: "" },children: [{ id: "0_4", name: "StartupVisa", data: { relation: "" }, children: [] }]},{ id: "0_5", name: "Community", data: { relation: "" },children: [{ id: "0_6", name: "Different Solutions", data: { relation: "" }, children: [] }]},{ id: "0_7", name: "News/media website", data: { relation: "" },children: [{ id: "0_8", name: "iSpazio", data: { relation: "" }, children: [] }]},{ id: "0_9", name: "Non-profit organization", data: { relation: "" },children: [{ id: "0_10", name: "Indigeni Digitali", data: { relation: "" }, children: [] }]},{ id: "0_11", name: "Website", data: { relation: "" },children: [{ id: "0_12", name: "Hot Pin Venezuela", data: { relation: "" }, children: [] },{ id: "0_13", name: "El Chiguire Bipolar", data: { relation: "" }, children: [] }]}], data: { relation: "some text" }}';

I then use this var (json) in a chart engine, the think is, if i copy the string returned by _rhraph() over the ajax hardcoding it on the javascript it works, it gets the string as an object and draw the chard. But if a let the script retrieve the var from the php script as you may suppose this var arrives as text on json = JSON.parse(result.rgraph); because if i do an alert on json i see the content and not Object object. So at this point I changed the code to: json = JSON.parse(result.rgraph);

But this gives me an error: Uncaught SyntaxError: Unexpected token i. I have no idea what is going on, seems like the json var is not correctly formatted but why when I hardcode the var it works? Any ideas?

4 Answers 4

1

You need to quote your keys for valid JSON

Sign up to request clarification or add additional context in comments.

Comments

1

That's not valid JSON. key names must be quoted, e.g:

{ "id" : ...

Comments

1

Just put all JSON keys in quotes ("id", "etc")...

Comments

0

I think you have to parse it with jQuery.parseJSON():

json = jQuery.parseJSON(result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.