georgina
(gja)
September 24, 2012, 2:35am
1
Hi,
I'm wanting to create a json service to enable information to be fetched from our CMS and displayed on various remote servers.
From http://api.jquery.com/jQuery.getJSON/ - it says
[quote]"$.getJSON("/path/to/jsonservice?jsoncallback=?", function…)
The "?" gets replaced with a value by JQuery, which the actual translated request the method ends up making is like:
$.getJSON("/path/to/jsonservice?jsoncallback=json1234", function…)
The jsonservice code then has to wrap the json code in the call back, like so:
Content-type: text/plain\n\n
json1234({ "attr1" : "value1" })
if the 'json1234(…)' does not exist in the json results, the $.getJSON will silently error."[/quote]
I can create the actual json via an asset listing but doing the wrapping part is more difficult.
Does matrix have any way to create a jsonservice?
Thanks Georgina.
synfield
(Waddy707)
October 18, 2012, 12:28am
2
[quote]
Hi,
I'm wanting to create a json service to enable information to be fetched from our CMS and displayed on various remote servers.
From http://api.jquery.com/jQuery.getJSON/ - it says
I can create the actual json via an asset listing but doing the wrapping part is more difficult.
Does matrix have any way to create a jsonservice?
Thanks Georgina.
[/quote]
Setting up a json service in Squiz is very easy.
First you create a design file as follows;
<MySource_PRINT id_name="global " var="content_type" content_type="application/json" />
<MySource_AREA id_name="page_body" design_area="body" >
<MySource_SET name="format" value="low_bandwidth" />
</MySource_AREA>
In your asset listing you would place the following as raw content in the page contents e.g
%globals_get_callback%({"count":"%asset_count%", "progslist":[%asset_listing%]})
and in your type formats e.g.
{ "heading" : "%asset_name%", "url" : "%asset_url%" },
Note the comma on the end which separates our list items, but will need to omit on our last item. Therefore we need to set a -1 position format that by selecting 'display formats' in 'right click' menu and setting up a link to 'position format' page which is identical to type formats accept that you omit the trailing comma.
Check th output of the asset list page to make sure it is in valid json format.
Then you should be able to call cross domain e.g.
$.getJSON('http://another.domain/programme-area-json-list3?callback=?' , function (data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
Check call using firbug console, and you should be good to go!