[xwiki-users] Generating pure JSON
I'd like to be able to generate pure JSON from a wiki page, to use in other pages via AJAX. To that end I created a page with the following content: {{velocity wiki="false"}} $response.setContentType('application/json') { "greeting" : "Hello", "location": "World" } {{/velocity}} Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code: <p><br/>{<br/>"greeting" : "Hello",<br/>"location": "World"<br/>}</p> I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain. If I remove the $response.setContentType('application/json') https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
Hi Bryn, On 17 Jan 2015 at 09:43:30, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
I'd like to be able to generate pure JSON from a wiki page, to use in other pages via AJAX. To that end I created a page with the following content: {{velocity wiki="false"}} $response.setContentType('application/json') { "greeting" : "Hello", "location": "World" } {{/velocity}}
Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code:
{ "greeting" : "Hello", "location": "World" }
I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain.
If I remove the $response.setContentType('application/json') https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
Inded, I would have imagine also that xpage=plain would have worked but it doesn’t. You have to use outputsyntax. For example: http://playground.xwiki.org/xwiki/bin/get/Main/VMA?outputSyntax=plain Note: You can go on a page with a livetable and check with Chrome dev tools or Firebug the requests done and you’ll see it uses that. Thanks -Vincent
Thanks so much for that. It did indeed work :-) So 'view' with xpage=plain&outputSyntax=plain or just 'get' with outputSyntax=plain does the job. Now I know what to look for, for posterity I've put this information into http://extensions.xwiki.org/xwiki/bin/view/Extension/Output+JSON FWIW I'd have rather put the license as something like "public domain", but the closest ("Do What The F*** You Want..") seemed unnecessarily vulgar. Does such language really have a place on your website? Thanks, Bryn ________________________________ From: Vincent Massol [[email protected]] on behalf of [email protected] [[email protected]] Sent: 17 January 2015 21:23 To: XWiki Users Cc: Bryn Jeffries Subject: Re: [xwiki-users] Generating pure JSON Hi Bryn, On 17 Jan 2015 at 09:43:30, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
I'd like to be able to generate pure JSON from a wiki page, to use in other pages via AJAX. To that end I created a page with the following content: {{velocity wiki="false"}} $response.setContentType('application/json') { "greeting" : "Hello", "location": "World" } {{/velocity}}
Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code:
{ "greeting" : "Hello", "location": "World" }
I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain.
If I remove the $response.setContentType('application/json') https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
Inded, I would have imagine also that xpage=plain would have worked but it doesn’t. You have to use outputsyntax. For example: http://playground.xwiki.org/xwiki/bin/get/Main/VMA?outputSyntax=plain Note: You can go on a page with a livetable and check with Chrome dev tools or Firebug the requests done and you’ll see it uses that. Thanks -Vincent
Hi Bryn, On 19 Jan 2015 at 02:41:00, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
Thanks so much for that. It did indeed work :-) So 'view' with xpage=plain&outputSyntax=plain or just 'get' with outputSyntax=plain does the job.
Now I know what to look for, for posterity I've put this information into http://extensions.xwiki.org/xwiki/bin/view/Extension/Output+JSON
cool thanks!
FWIW I'd have rather put the license as something like "public domain", but the closest ("Do What The F*** You Want..") seemed unnecessarily vulgar. Does such language really have a place on your website?
I agree it looks vulgar. However this is a real license, see http://www.wtfpl.net/about/ and http://en.wikipedia.org/wiki/WTFPL Thanks -Vincent
Thanks,
Bryn
From: Vincent Massol [[email protected]] on behalf of [email protected] [[email protected]] Sent: 17 January 2015 21:23 To: XWiki Users Cc: Bryn Jeffries Subject: Re: [xwiki-users] Generating pure JSON
Hi Bryn,
On 17 Jan 2015 at 09:43:30, Bryn Jeffries ([email protected](mailto:[email protected])) wrote:
I'd like to be able to generate pure JSON from a wiki page, to use in other pages via AJAX. To that end I created a page with the following content: {{velocity wiki="false"}} $response.setContentType('application/json') { "greeting" : "Hello", "location": "World" } {{/velocity}}
Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code:
{ "greeting" : "Hello", "location": "World" }
I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain.
If I remove the $response.setContentType('application/json') https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
Inded, I would have imagine also that xpage=plain would have worked but it doesn’t. You have to use outputsyntax.
For example: http://playground.xwiki.org/xwiki/bin/get/Main/VMA?outputSyntax=plain
Note: You can go on a page with a livetable and check with Chrome dev tools or Firebug the requests done and you’ll see it uses that.
Thanks -Vincent
On Sat, Jan 17, 2015 at 10:43 AM, Bryn Jeffries <[email protected]> wrote:
I'd like to be able to generate pure JSON from a wiki page, to use in other pages via AJAX. To that end I created a page with the following content: {{velocity wiki="false"}} $response.setContentType('application/json')
{ "greeting" : "Hello", "location": "World" }
Instead of printing the JSON directly I recommend using the $jsontool (see http://extensions.xwiki.org/xwiki/bin/view/Extension/Velocity+Module#HVeloci... ). The advantage is that you don't have to care about escaping JSON (property) values. $jsontool.serialize({ "greeting" : $someObject.greeting, "location": $someOtherObject.location }) Hope this helps, Marius
{{/velocity}}
Calling this via /xwiki/bin/get/Sandbox/JsonTest I get the following code:
<p><br/>{<br/>"greeting" : "Hello",<br/>"location": "World"<br/>}</p>
I get the same via /xwiki/bin/view/AlertDB/JsonTest?xpage=plain.
If I remove the $response.setContentType('application/json') https://dev.alertnessdb.net/xwiki/bin/view/AlertDB/JsonTest?xpage=plain
_______________________________________________ users mailing list [email protected] http://lists.xwiki.org/mailman/listinfo/users
participants (3)
-
Bryn Jeffries -
Marius Dumitru Florea -
vincent@massol.net