Problem with Ajax

33 views
Skip to first unread message

Tom

unread,
Mar 1, 2012, 3:06:56 PM3/1/12
to amber-lang
Hi,
I try some asynchronous data fetching using the Picasa API. The
response from Picasa is an atom feed, of which I'd like to pick the
images' urls. So I founded a new class "PicasaQuery", where instances
can be configured to a certain query of images and then fetch the
search result from the Picasa website. Therefor I have a method
"execute", which constructs the request url and does the query. The
query's response is kept in the instance variable "responseXML".

Object subclass: #PicasaQuery
class instanceVariableNames: 'baseURL'

instanceVariableNames: 'query maxResults responseXML'
package: 'TB-PicasaAPI'


execute
"tb: If maxResult is set, then execute the query and answer
the response markup or an empty string."
| uri |
responseXML := ''.
maxResults notNil ifTrue: [
uri := (self class baseURL),'q=',query,'&max-results=',maxResults.
jQuery ajax: uri options: #{
'accept' -> 'application/xml'.
'success' -> [ :data :txtStatus :xhr | responseXML := data ].
'error' -> [window alert: 'Request failed at: ', uri]
}
].
^responseXML


My test query goes like...

response := PicasaQuery new
query: 'Frank+Zappa';
maxResults: 10;
execute.

...where execute returns the response or an empty string. The uri in
this method works fantastic in any web browser, but all that I get
here, is that error alert.

Any suggestions?

Regards...

Pablo Iaria

unread,
Mar 1, 2012, 3:50:35 PM3/1/12
to amber...@googlegroups.com
Hola,

You should handle correctly the acync callback. For example by changing the ajax callback to invoke a #responseXML: method:

 execute
       "tb: If maxResult is set, then execute the query and answer
       the response markup or an empty string."
       | uri |
       maxResults notNil ifTrue: [
               uri := (self class baseURL),'q=',query,'&max-results=',maxResults.
               jQuery ajax: uri        options: #{
                       'accept' -> 'application/xml'.
                       'success'  -> [ :data :txtStatus :xhr | self responseXML: data ].

                       'error' -> [window alert: 'Request failed at:  ', uri]
               }
       ].

Saludos!
Pablo.-

Tom

unread,
Mar 1, 2012, 4:44:48 PM3/1/12
to amber-lang
Hi and thank You!

I understand Your objection, but pitty it does not solve the problem.
I have tried different 'accept' types like 'application/xhtml+html'
and 'application/atom+xml', but with no success either.

Cheers...

Tom

unread,
Mar 1, 2012, 6:49:02 PM3/1/12
to amber-lang
Hm... Using firebug reveals a respose header with status="200 ok" but
the response XML is empty :(

Cheers...

Felix Franz

unread,
Mar 2, 2012, 4:43:32 AM3/2/12
to amber...@googlegroups.com
has "self class baseURL" the same domain/protocol as your page? If not you are blocked because
of the "same origin policy". See for example the documentation of jquery.ajax:
"Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol."

(http://api.jquery.com/jQuery.ajax/ or http://en.wikipedia.org/wiki/Same_origin_policy).

Cheers,

Felix

Tom

unread,
Mar 2, 2012, 8:32:09 AM3/2/12
to amber-lang
es! It's true.

I'm stuck in the cross domain access restriction. So, I understand
that I must not use Ajax here. What other ways of retrieving contents
in the background are available? Do I have to fiddle with a 1pt
<iframe> ?

Cheers...

On Mar 2, 10:43 am, Felix Franz <f...@gmx.net> wrote:
> has "self class baseURL" the same domain/protocol as your page? If not you are blocked because
> of the "same origin policy". See for example the documentation of jquery.ajax:
>   "Due to browser security restrictions, most "Ajax" requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol."
>
> (http://api.jquery.com/jQuery.ajax/orhttp://en.wikipedia.org/wiki/Same_origin_policy).

Paul DeBruicker

unread,
Mar 2, 2012, 9:24:48 AM3/2/12
to amber...@googlegroups.com, amber-lang
Maybe this library can help:

http://easyxdm.net/wp/

Amber Milan Eskridge

unread,
Mar 2, 2012, 9:32:11 AM3/2/12
to amber...@googlegroups.com
For every request, add a script-tag to the dom, that fetches the desired json. Strangly that is not prohibited.
Reply all
Reply to author
Forward
0 new messages