As was noted previously, you cannot use the standard XMLHttpRequest
which is at the prototype of the AJAX functionality built into
prototype and most other JS libraries. The way that Google gets
around this is by utilizing a method called JSONP, whereby you
dynamically add a script element to your document's head, which
contacts the server. The server then returns a block of Javascript
code, calling a callback function and passing it arguments that
represent the server's response. In the case of the AJAX APIs, you
can utilize JSONP by calling document.createElement('script');
assigning it the appropriate type attribute, and passing it a url like
this:
http://ajax.googleapis.com/ajax/services/search/local?sll=28.2117,77.2629&mrt=localonly&q=DLF%20Building&v=1.0&rsz=large&callback=myCallback
Notice the inclusion of the callback argument at the end of the url
you had provided. The function myCallback will be called when the
server responds, and it will be passed an object literal, as
documented here:
http://code.google.com/apis/ajaxsearch/documentation/reference.html
Jeremy R. Geerdes
Effective website design & development
Des Moines, IA
For more information or a project quote:
http://jgeerdes.home.mchsi.com
http://jgeerdes.blogspot.com
http://jgeerdes.wordpress.com
[email protected]
Unless otherwise noted, any price quotes contained within this
communication are given in US dollars.
If you're in the Des Moines, IA, area, check out Debra Heights
Wesleyan Church!
And check out my blog, Adventures in Web Development, at
http://jgeerdes.blogspot.com
!
On Jun 6, 2009, at 8:16 PM, Kaveh Rassoulzadegan wrote:
> Hello,
>
> Prototype.js can not retrieve cross-domain ajax results because of
> security reasons.
> You may find few hacks(script tag hack) to do this client-side, try
> to googling 'cross-domain ajax request' but you may run into further
> issues.
> Your ajax code could target one of your server-side pages though.
> Example for php, a page simply containing this:
>
> <?
> $your_url='http://ajax.googleapis.com/ajax/services/search/local?sll=28.2117,77.2629&mrt=localonly&q=DLF%20Building&v=1.0&rsz=large'
>
> ;
> echo file_get_contents($your_url);
> ?>
>
> Note that you can pass url values with post or get to your server
> php page ($_GET or $_POST).
>
>
> ...and then in your previous code:
>
> new Ajax.Request('/page_on_same_domain.php', {method:'get'...
>
> Finally, I do not see any reason why you declare a second argument
> variable 'json' for your 'reportSuccess' function, as you declare it
> in its body.
> Just pass 'transport'.
>
> kv
>
> sumitarora a écrit :
>>
>> I’m trying to call Google Search RESTful API using javascript
>> prototype library.
>>
>> Here is my code:
>>
>> new Ajax.Request('http://ajax.googleapis.com/ajax/services/
>> search/local?sll=28.2117,77.2629&mrt=localonly&q=DLF
>> %20Building&v=1.0&rsz=large',
>> {
>> method:'get',
>> requestHeaders: {Accept: 'application/json'},
>> onSuccess: reportSuccess,
>> onFailure: reportError
>> });
>>
>> function reportError(request)
>> {
>> alert(request);
>>
>> }
>> function reportSuccess(transport, json)
>> {
>> var json = transport.responseText.evalJSON(true);
>> }
>>
>> But it returns nothing.
>>
>> Is there any way to call RESTful APIs using javascript, I don’t want
>> to use Google’s javascript controls.
>>
>> Thanks,
>> Sumit Arora
>>
>>
>>
>>
>>
>> No virus found in this incoming message.
>> Checked by AVG - www.avg.com
>> Version: 8.5.339 / Virus Database: 270.12.54/2158 - Release Date:
>> 06/06/09 05:53:00
>>
>>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google AJAX APIs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/Google-AJAX-Search-API?hl=en
-~----------~----~----~----~------~----~------~--~---