Brython, ajax, json, jQuery,...

255 views
Skip to first unread message

Kiko

unread,
Jun 4, 2013, 5:59:50 PM6/4/13
to bry...@googlegroups.com
Hi all,

Yesterday I was playing with ajax() and json module to try to get json data from a call to the youtube api:

Example: http://gdata.youtube.com/feeds/api/videos/xhLxnlNcxv8?v=2&alt=json-in-script&callback=listInfo, it is just an example!! :-)

I was trying with what is written in this examples: http://brython.info/gallery/ajax.html

I also tried to use the getJSON from the jQuery library with no success.

Mi idea was something similar to the described in this link but with Brython:
http://stackoverflow.com/questions/10066638/get-youtube-information-via-json-for-single-video-not-feed-in-javascript

I also tried to get a remote txt file but ajax only worked with local files.

Maybe this is very simple but my understanding of JS is very close to zero and I couldn't figure out how to do it with Brython.

If someone could lend a hand with this.

Thanks in advance.

Olemis Lang

unread,
Jun 5, 2013, 11:25:40 AM6/5/13
to bry...@googlegroups.com
On 6/4/13, Kiko <kikoco...@gmail.com> wrote:
> Hi all,
>

:)

[...]
>
> I also tried to get a remote txt file but ajax only worked with local
> files.
>
> Maybe this is very simple but my understanding of JS is very close to zero
> and I couldn't figure out how to do it with Brython.
>
> If someone could lend a hand with this.
>

Maybe this is a consequence of browser's same origin policy ?

--
Regards,

Olemis.

Apache™ Bloodhound contributor
http://issues.apache.org/bloodhound

Blog ES: http://simelo-es.blogspot.com/
Blog EN: http://simelo-en.blogspot.com/

Featured article:

Kiko

unread,
Jun 5, 2013, 12:01:01 PM6/5/13
to bry...@googlegroups.com



2013/6/5 Olemis Lang <ole...@gmail.com>

On 6/4/13, Kiko <kikoco...@gmail.com> wrote:
> Hi all,
>

:)

[...]
>
> I also tried to get a remote txt file but ajax only worked with local
> files.
>
> Maybe this is very simple but my understanding of JS is very close to zero
> and I couldn't figure out how to do it with Brython.
>
> If someone could lend a hand with this.
>

Maybe this is a consequence of browser's same origin policy ?

I suppose. But it could be possible the behavior of jQuery.getJSON() (http://api.jquery.com/jQuery.getJSON/) in Brython? I tried using JSObject and jQuery but I couldn't.

Amirouche Boubekki

unread,
Jun 5, 2013, 12:02:12 PM6/5/13
to bry...@googlegroups.com



2013/6/5 Kiko <kikoco...@gmail.com>
Hi,

Do you see any call done in firebug (or similar) ? what is the response ?

Francois Dion

unread,
Jun 5, 2013, 12:45:10 PM6/5/13
to bry...@googlegroups.com
1) Since you are calling cross domain, you cant get JSON data directly. You can do it through a proxy on your domain, or with a callback.

2) You are calling the JSON interface with a callback, so you will get back JSONP:
// API callback
listInfo({"version":"1.0","encoding":"UTF-8","entry":{"xmlns$app":"http://www.w3.org/2007/app","xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/", ...

py_ajax.js is a thin layer above xmlhttprequest. It doesn't have the logic to parse the URL and see a &callback=? and recognize that the data coming back is really javascript (the function name being the callback name you pass). We then would need to add code to create a <script src=""> using the url instead of using xmlhttprequest and get the callback (previously defined) executed with the data as arg. Not exactly trivial, but as far as I know it is the only way to do this all client side. You also have to trust the service you are calling, obviously.

Finally, if you are running without a web server, then only local files can be fetched with ajax.

Francois
--
www.pyptug.org  -  raspberry-python.blogspot.com  -  @f_dion


--
You received this message because you are subscribed to the Google Groups "brython" group.
To unsubscribe from this group and stop receiving emails from it, send an email to brython+u...@googlegroups.com.
To post to this group, send email to bry...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/brython/CAB-sx63MhHY%3DrijtO0d3cJ1A_5i1-SNGYnLAHLnRKo%2BkEX23cw%40mail.gmail.com?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Kiko

unread,
Jun 5, 2013, 12:50:37 PM6/5/13
to bry...@googlegroups.com



2013/6/5 Francois Dion <franco...@gmail.com>

1) Since you are calling cross domain, you cant get JSON data directly. You can do it through a proxy on your domain, or with a callback.

2) You are calling the JSON interface with a callback, so you will get back JSONP:
// API callback
listInfo({"version":"1.0","encoding":"UTF-8","entry":{"xmlns$app":"http://www.w3.org/2007/app","xmlns":"http://www.w3.org/2005/Atom","xmlns$media":"http://search.yahoo.com/mrss/", ...

py_ajax.js is a thin layer above xmlhttprequest. It doesn't have the logic to parse the URL and see a &callback=? and recognize that the data coming back is really javascript (the function name being the callback name you pass). We then would need to add code to create a <script src=""> using the url instead of using xmlhttprequest and get the callback (previously defined) executed with the data as arg. Not exactly trivial, but as far as I know it is the only way to do this all client side. You also have to trust the service you are calling, obviously.

Finally, if you are running without a web server, then only local files can be fetched with ajax.

Thank you, Francois, for the explanation. Now, it makes sense.

Reply all
Reply to author
Forward
0 new messages