Hi,

> Any suggestions?  Is this possible/doable?

Unfortunately, as far as I can tell on a few minutes' effort, TinyURL
doesn't support cross-origin resource sharing (CORS)[1], and so while
you could do this with Ajax.Request[2]:

    new Ajax.Request("http://tinyurl.com/api-create.php?url=http://
www.google.com/", {
        method:    "GET",
        onSuccess: function(response) {
            var newUrl = response.responseText;
            alert("New URL: " + newUrl);
        }
    });

...it doesn't work because it fails afoul of the SOP[3].

Apparently you're not the first to want to do this client-side,
because in the course of trying to find out whether TinyURL supported
CORS I ran across a Google AppEngine site[4] that works around this
using JSONP[5]. So you could use that service (if you could find
someone to ask permission from):

    function gotTinyURL(data)
    {
        if (data.ok) {
            alert("New URL: " + data.tinyurl);
        }
        else {
            alert("Request failed");
        }
    }

    function getTinyURL(url)
    {
        var head, script;

        head = $$('head')[0];
        if (head) {
            script = new Element('script', {
                type: 'text/javascript',
                src: 'http://json-tinyurl.appspot.com/?url=' + url +
"&callback=gotTinyURL"
            });
            head.appendChild(script);
            head.removeChild(script);
        }
    }

Note that when you call a JSONP service in this way, you are *trusting
that service not to do anything bad* because you're giving them full
access to the page (that's how JSONP works).

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


[1] http://www.w3.org/TR/access-control/
[2] http://api.prototypejs.org/ajax/ajax/request/
[3] http://en.wikipedia.org/wiki/Same_origin_policy
[4] http://json-tinyurl.appspot.com/
[5] http://en.wikipedia.org/wiki/JSON#JSONP

On Apr 6, 8:25 pm, mTorbin <[email protected]> wrote:
> Hey all,
>
> I'm relatively new to prototype.js and I'm attempting to figure out
> the ajax.response() function.  I'd like to build a little function
> that achieves the same as PHP's file_get_contents() but using
> Javascript.  Specifically I'd like to be able to take advantage of
> TinyURL's open API:
>
> http://tinyurl.com/api-create.php?url=http://www.google.com/
>
> Any suggestions?  Is this possible/doable?
>
> Thanks in advance!
>
>  - MT

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" 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/prototype-scriptaculous?hl=en.

Reply via email to