I finally got this working. Setting the content-type did the trick (thanks
Diez). Now when I invoke the url directly in the browser I get an xml
formatted page in response. With regards to the parameter segVal, the way I
show it in my example works (XMLHttpRequestObject.open('GET',
"${tg.url('/sponsor/getDivisions/" + segVal + "')}");) because it gets
passed to the (python) controller in order to query the database, and not
sent to a html page which would require ".../?segmentid=" + segVal + "...".
Although the xml method is working, it seems from what I have read that json
would be a better way to handle it. To convert the data to json in the
controller, is it just a matter of formatting it with the curly brackets
etc. ({"foo" : "bar" }), or do I need to use some other functions for the
conversion? And do I need to include @expose('json')?
Thanks.
On Wed, Jul 14, 2010 at 5:53 PM, Diez B. Roggisch <[email protected]> wrote:
>
> Am 14.07.2010 um 05:50 schrieb Michael Pearce:
>
>
> This is my controller which (attempts to) create the xml doc from the
>> database:
>>
>> @expose()
>> def getDivisions(self, segmentid, *args, **kw):
>> def genAddElement( division ):
>> return """ <DIVISION>
>> <ID>%d</ID>
>> <NAME>'%s'</NAME>
>> <SEGMENTID>%d</SEGMENTID>
>> </DIVISION> """ % ( division.divisionid, division.name,
>> division.segmentid )
>>
>> divisions =
>> DBSession.query(Division).filter(Division.segmentid==int(segmentid)).order_by(Division.name).all()
>> xmlElements = map( genAddElement, divisions )
>> xmlStr = string.join( xmlElements, '\n' )
>> xmlDoc = """ <?xml version='1.0' encoding='ISO-8859-1'?>
>> <DIVISIONS>
>> %s
>> </DIVISIONS> """ % (xmlStr)
>> return xmlDoc
>> My javascript goes something like this:
>>
>
> This is amongst the worst ways of generating XML, but that's just for the
> record. And a reason to use the much more compact JSON.
>
> One other problem is that you don't set a proper content-type. Do that
> using e.g.
>
> @expose(content_type="text/xml")
>
> And last but not least, what does invoking the url directly in the browser
> give you (with a proper parameter of course)?
>
> Diez
>
>
> --
> 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]<turbogears%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/turbogears?hl=en.
>
>
--
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.