Freeboard / freeboard

A damn-sexy, open source real-time dashboard builder for IOT and other web mashups. A free open-source alternative to Geckoboard.
http://freeboard.io
MIT License
6.45k stars 1.2k forks source link

Using JSONP breaks parsing of return data #27

Closed antonfh closed 10 years ago

antonfh commented 10 years ago

Good day,

Maybe I missed it in the notes or other questions, but I can not see any mention of the following. To fix CORS, I am using JSONP, but now I am getting errors on the returned data :

Timestamp: 2014/05/30, 14:45:45 Error: SyntaxError: missing ; before statement Source File: http://www.site.cc/rand?callback=jQuery20302740955555920611_1401453890633&_=1401453890657 Line: 1, Column: 8 Source Code: {"total":62}

basically indicating that between total and 62 (at the colon) there is a missing ; before statement ?

Worked fine on JSON when running both data and freeboard on same server/site

Thanks for the time and effort.

jheising commented 10 years ago

I'm getting a 404/notfound error on that link. Can you send me a valid one? Thanks.

antonfh commented 10 years ago

Hi sorry, yes that is on a local test (not public facing/access) basically its only an api that returns the data element (json_encoded) as {"total":"40"} , or another call returns something like {"id":"1","name":"my name","total":"1") ..... which results in same error indicating that the very first : is where a missing ; before statement must be .... how must a custom plugin/ data source be defined (I have one obviously calling these apis) which used types JSON but now I define JSONP (so not sure if a datasource which worked/works with JSON can simply just change type to JSONP (or is there some changes that must be made to handle JSONP ?

Thanks

jheising commented 10 years ago

Oh, I think the problem is that your JSONP file isn't really JSONP. http://en.wikipedia.org/wiki/JSONP. JSONP is actually a script file that must be formatted like javascript and calls back the function passed in the URL. So for that example above, the file would need to look like:

jQuery20302740955555920611_1401453890633({ "total": 62 });

The main thing is that the function name changes randomly so you need to make sure your API endpoint is taking this into account when it creates the JSONP file.