Get json object from asset listing


(Mahearnpad) #1

Hi


I'm using an asset lister to make a json object from the assetids of the standard pages under a nominated root node. The result page of the asset lister returns the correct values:

{{"assetid":"134732"}, {"assetid":"134733"}, {"assetid":"146018"}, {"assetid":"146039"}, {"assetid":"146040"}, {"assetid":"146049"}, {"assetid":"146050"}, {"assetid":"146017"}}



but when I try to get these values returned from a jQuery getJSON call, it fails. My code is as follows:


    
    var getURL = "./?a=138204"; // Retrieve standard page assetids from asset lister (json format)
    		$.getJSON(getURL, function( data ){
    			$.each(data, function(key, val){
    				$('#listing').append(val + "");
console.log(val); }); }) .error(function( ) { console.log("error"); });


I've checked permissions, and the page is accessible to public read. I've run out of ideas. Anyone?

(Nic Hubbard) #2

What do you see if you add an alert(data) inside of $.getJSON?


(Mahearnpad) #3

[quote]
What do you see if you add an alert(data) inside of $.getJSON?

[/quote]



Hi Nick

data is not returned - no success - I'm just getting error.


(Nic Hubbard) #4

[quote]
Hi Nick

data is not returned - no success - I'm just getting error.

[/quote]



Are you sure that the ./?a= url style is getting converted to a real URL? I would use %globals_asset_url:138204% instead.


(Mahearnpad) #5

[quote]
Are you sure that the ./?a= url style is getting converted to a real URL? I would use %globals_asset_url:138204% instead.

[/quote]



According to source, it is converting correctly, but jic, I tried the full URL, still no luck.

Put some error handling stuff in and getting this back, but not sure what is causing the "unexpected token":



200

SyntaxError

arguments: Array[1]

get message: function () { [native code] }

get stack: function () { [native code] }

set message: function () { [native code] }

set stack: function () { [native code] }

type: "unexpected_token"

proto: Error


(Anthony Barnes) #6

[quote]
Put some error handling stuff in and getting this back, but not sure what is causing the "unexpected token":

[/quote]



Unexpected token means the json object is broken. In the sample you posted your nested objects don't contain a property. I would look at changing the format of your JSON, eg:


    
    {data: [{"assetid":"134732"}, {"assetid":"134733"}, {"assetid":"146018"}, {"assetid":"146039"}, {"assetid":"146040"}, {"assetid":"146049"}, {"assetid":"146050"}, {"assetid":"146017"}]}

(Mahearnpad) #7

[quote]
Unexpected token means the json object is broken. In the sample you posted your nested objects don't contain a property. I would look at changing the format of your JSON, eg:


    
    {data: [{"assetid":"134732"}, {"assetid":"134733"}, {"assetid":"146018"}, {"assetid":"146039"}, {"assetid":"146040"}, {"assetid":"146049"}, {"assetid":"146050"}, {"assetid":"146017"}]}

[/quote]



Thanks Anthony, that sounds like the answer.


(Mahearnpad) #8

Actually, to complicate matters, I've just noticed that the asset lister is returning html - I think this is the problem.


(Nic Hubbard) #9

[quote]
Actually, to complicate matters, I've just noticed that the asset lister is returning html - I think this is the problem.

[/quote]



Yeah, you need to apply a special Design asset to the listing. Basically a blank design with a json content type.


(Mahearnpad) #10

[quote]
Yeah, you need to apply a special Design asset to the listing. Basically a blank design with a json content type.

[/quote]



Thanks Nick



I ended up nesting the asset listing inside a script tag and getting the object from the DOM. But I'll use your suggestion and try the xhr method again - much less complicated.



Michael


(Nic Hubbard) #11

[quote]
Thanks Nick



I ended up nesting the asset listing inside a script tag and getting the object from the DOM. But I'll use your suggestion and try the xhr method again - much less complicated.



Michael

[/quote]



Just use the following in your Design:


    
    

(Mahearnpad) #12

[quote]
Just use the following in your Design:


    
    

[/quote]





Nic, we tried this and it still didn't work. The error indicates a "<" in the first index of the returned array.

This is a one-off script, so I'll stick with my nested content solution - don't have any time to stuff around.



It would be good to know why the other isn't working though, for future reference.


(Nic Hubbard) #13

[quote]
Nic, we tried this and it still didn't work. The error indicates a "<" in the first index of the returned array.

This is a one-off script, so I'll stick with my nested content solution - don't have any time to stuff around.



It would be good to know why the other isn't working though, for future reference.

[/quote]



Can you give me a link to the page?


(Mahearnpad) #14

[quote]
Can you give me a link to the page?

[/quote]



Can't, it's on a server that's not yet publicly available - we're doing a website redev. I can supply source code from the browser if that helps.


(Nic Hubbard) #15

[quote]
Can't, it's on a server that's not yet publicly available - we're doing a website redev. I can supply source code from the browser if that helps.

[/quote]



Yeah, source would be great.


(Mahearnpad) #16

[quote]
Yeah, source would be great.

[/quote]



I've just realised that the asset lister with the json in it doesn't have a url, as it's in a folder outside our new site asset. This may be why the getJSON method is failing.



M


(Nic Hubbard) #17

[quote]
I've just realised that the asset lister with the json in it doesn't have a url, as it's in a folder outside our new site asset. This may be why the getJSON method is failing.



M

[/quote]



Yeah, it needs a URL for it to work with that jQuery method.