Problem with Remote JSON

44 views
Skip to first unread message

NadAF

unread,
Aug 3, 2009, 1:20:22 AM8/3/09
to Timemap.js Development
Hi,
I am trying to use Timemap with remote JSON code. The problem is that
my code is never downloaded. I tried to serve .js file directly but it
does not work. I tried to modify HTTP response content-type to "text/
javascript" and "charset=utf-8", but it is not working either!

What is going wrong here ?

Thanks!

Nick Rabinowitz

unread,
Aug 3, 2009, 12:23:05 PM8/3/09
to nade...@gmail.com, Timemap.js Development
Hello -

Some cut-and-pasting from a recent email on a similar question (I should probably make this a wiki page sometime soon):

The idea here is a JSONP model: http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/

This won't work unless your JSON is being generated by a script (e.g. in PHP) rather than a static file. The idea is that you specify a function name in the URL parameters, then repeat that in the script - so if the url is

http://www.example.com/mydata.php?callback=SPAM

the script would return

SPAM(...some data...);

so that the function SPAM() gets called. Timemap.js will generate the function name automatically - the first time you load data, it'll be TimeMap.loaders.jsonp._0, then ._1, etc  - so all you need to supply as a url is "http://www.example.com/mydata.php?callback=" - but then you're responsible for writing the script so that it takes the "callback" parameter and puts it into the output, e.g. (in PHP):

$func = $_GET['callback'];
echo $func . '(' . $myjsonstring . ');';

To load data from a separate JSON file, without messing with any JSONP issues, there are two ways: the easy way and the harder way. The harder way is to use the json_string loader, which requires the development version of timemap.js (version 1.5, not yet released - I should be releasing it soon, but it's likely to be another week or two - to get it now, you'll need to get it using a subversion program: http://code.google.com/p/timemap/source/checkout). You can see the loading syntax here:

http://code.google.com/p/timemap/source/browse/trunk/tests/loadTests.js#160

and what the file might look like here:

http://code.google.com/p/timemap/source/browse/trunk/tests/data/data_string.js

But if you don't need to pass any parameters to the JSON file (that is, if you are always going to want the same set of data), then you can do it the easy way, which is just to declare it in a global variable in a separate file:

var myItems = [{...item 1...}, etc]

Then, when you call TimeMap.init(), just use the "basic" loader and load your variable:

    datasets: [
            {
                title: "Test Dataset",
                id: "test",
                data: {
                    type: "basic",
                    value: myItems
                }
            }
     ]

I hope that helps.

-Nick

NadAF

unread,
Aug 4, 2009, 2:03:06 AM8/4/09
to Timemap.js Development
Many thanks, Nick!
It is working now! I am using ASP.NET by the way not PHP!

It would be great if you could publish all those helpful things in a
single PDF file and put it on the main project page. This will avoid
repeating same questions again and again!
> http://code.google.com/p/timemap/source/browse/trunk/tests/loadTests....
>
> and what the file might look like here:
>
> http://code.google.com/p/timemap/source/browse/trunk/tests/data/data_...
>
> But if you don't need to pass any parameters to the JSON file (that is, if
> you are always going to want the same set of data), then you can do it the
> easy way, which is just to declare it in a global variable in a separate
> file:
>
> var myItems = [{...item 1...}, etc]
>
> Then, when you call TimeMap.init(), just use the "basic" loader and load
> your variable:
>
>     datasets: [
>             {
>                 title: "Test Dataset",
>                 id: "test",
>                 data: {
>                     type: "basic",
>                     value: myItems
>                 }
>             }
>      ]
>
> I hope that helps.
>
> -Nick
>
Reply all
Reply to author
Forward
0 new messages