Hi again,
I have made a simple solution for this which I will share here for
others to improve on. I have not tried to play with the internals of
the expose decorator since that is beyond my current knowledge of TG1
and since I plan to go to TG2 at some point. My approach is that I
have changed my previous @expose("json") decorators to @expose()
decorators and do the JSON conversion myself using a method which pads
the jsonp argument when present (I am currently not wrapping the reply
in () when jsonp='' since I don't see any reason for this and because
it makes it a bit more difficult to parse the result when not using
jsonp):
def jsonp_encode(data, jsonp):
"""JSON encode data - optionally with JSONP."""
if jsonp != '':
result = jsonp + '(' + encode(data) + ')'
else:
result = encode(data)
return result
This method is placed in the json.py and encode is from
turbojson.jsonify.
Best regards,
Jesper
On Jun 11, 11:58 am, jlar <[email protected]> wrote:
> Hi TG users and developers,
>
> I have a TG application which presents weather forecasts for users.
> The novelty of the application is that it is actually extracting data
> from the meteorological forecast data files and plotting the forecasts
> when the user requests it. This allows for example users to extract a
> weather forecast along a route they plan to follow or a time series
> interpolated to the exact point that the user wants. So far the only
> way to use this is through my own web pages (in testing):
>
> http://www.worldwildweather.com
>
> I think these data could be used in a lot of different situations (for
> example route planning software could plot the weather along the route
> with high accuracy). I have therefore decided some time ago to make
> the data and plots available through a HTTP request with TG returning
> a JSON response. This interface is more or less finished. I am however
> lacking one important detail.
>
> Web developers who wants to access this service using Javascript on
> the client side cannot do so directly due to the Javascript security
> model (server side you can of course access it using whatever language
> you want). There are however methods for developers to access
> "trusted" remote sites. I believe a good and and widely adapted (by
> Javascript libraries as far as I understand) solution is the one
> proprosed by Bob Ippolito. This solution is named JSONP for JSON with
> Padding and requires a small server side modification. What needs to
> be done is basically to wrap the output JSON in parantheses and if
> jsonp is present as a query argument to insert the text value from
> that before the first (. Or to put it differently: If jsonp=mycallback
> - then the response should be:
>
> mycallback(output_json)
>
> and if jsonp is not specified it should return:
>
> (output_json)
>
> http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
>
> Before reinventing the wheel I wanted to ask you if anyone has already
> done this work? If not I could sure use some recommendations on how to
> proceed.
>
> Best regards,
> Jesper
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---