Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HTMLCHARTS for showing the PIE CHART

Uncaught SyntaxError: Unexpected token n

[{name: '62098369', y :9000} , {name: '62098319', y :6635.28} , {name: '62098318', y :4705.88} , {name: '62098307', y :4200} , {name: '62098368', y :3600} , {name: '62098317', y :2353.17} , {name: '62098316', y :2352.94} , {name: '61090526', y :2250} , {name: '62098306', y :2100} , {name: '62098308', y :2100} ]

this same above json data if pass statically as

var str= [{name: '62098369', y :9000} , {name: '62098319', y :6635.28} , {name: '62098318', y :4705.88} , {name: '62098307', y :4200} , {name: '62098368', y :3600} , {name: '62098317', y :2353.17} , {name: '62098316', y :2352.94} , {name: '61090526', y :2250} , {name: '62098306', y :2100} , {name: '62098308', y :2100} ];

series: [{
name: "GrossInvestment",
data: str,
colorByPoint: true
}]
it works fine but when i call dynamically it shows an error

Uncaught SyntaxError: Unexpected token n

Thanks
Posted
Comments
Kornfeld Eliyahu Peter 5-Jan-16 6:36am    
You should show the part of the code, that initialize str dynamically...
Member 10902837 5-Jan-16 10:08am    
my dynamic data for the str is

<%=strr %>

var str = document.getElementById("piedata").innerHTML;

if i use the str then it shows and error but when i copy the data of a tag which come as output and store it as statically in the javascript and call that in the series then it works

my Dynamic data will be for the a tag is
[{name: '62098369', y :9000} , {name: '62098319', y :6635.28} , {name: '62098318', y :4705.88} , {name: '62098307', y :4200} , {name: '62098368', y :3600} , {name: '62098317', y :2353.17} , {name: '62098316', y :2352.94} , {name: '61090526', y :2250} , {name: '62098306', y :2100} , {name: '62098308', y :2100} ]
Member 10902837 6-Jan-16 4:40am    
when i Parse the string it shows me this error

Uncaught SyntaxError: Unexpected token n
Nathan Minier 5-Jan-16 8:06am    
Seems to me that your data is not being passed properly. Assuming that you're fetching it from a network resource, are you passing the data or a promise? Are you using any frameworks?

1 solution

There is a big difference between a JavaScript object written as JSON (like [{},{}]) and a string containing a JSON formatted string (like "[{},{}]")...
In your case the innerHTML returns a string, but you try to parse later as JSON object...but when you are putting it there manually you are actually creating a JSON object...
So, what you have to do is to parse the content of innerHTML to turn it into a JSON object...
JavaScript
var str = JSON.parse(document.getElementById("piedata").innerHTML); 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900