0

I'm playing a bit with the goo.gl API and Javascript using the jsonlib like this:

function googl(url, cb) {
    jsonlib.fetch({
        url: 'https://www.googleapis.com/urlshortener/v1/url?key=<my-api-key>',
        header: 'Content-Type: application/json',
        data: JSON.stringify({longUrl: url})
    }, function (m) {
        var result = null;
        try {
            result = JSON.parse(m.content).id;
            if (typeof result != 'string') {
                result = null;
            }
        } catch (e) {
            result = null;
        }
        cb(result);
    });
}

But when I try to run this, I'm getting this error:

ReferenceError: Can't find variable: jsonlib_cb_1307278663586 - fetch:1

The content of fetch:1:

jsonlib_cb_1307278663587({"url": "https://www.googleapis.com/urlshortener/v1/url?key=<my-api-key>", "headers": {"via": "HTTP/1.1 GWA", "x-content-type-options": "nosniff", "x-google-cache-control": "remote-fetch", "expires": "Fri, 01 Jan 1990 00:00:00 GMT", "server": "GSE", "x-xss-protection": "1; mode=block", "etag": "\"0CNIIHrQYpIA69r1Pk9QCe1kzwI/u2pHqKdqG-dNhbJgEDlvIXi9lWk\"", "pragma": "no-cache", "cache-control": "no-cache, no-store, max-age=0, must-revalidate", "date": "Sun, 05 Jun 2011 13:14:52 GMT", "x-frame-options": "SAMEORIGIN", "content-type": "application/json; charset=UTF-8"}, "original-encoding": "iso-8859-1", "content": "{\n \"kind\": \"urlshortener#url\",\n \"id\": \"http://goo.gl/45cs\",\n \"longUrl\": \"http://developer.android.com/guide/practices/ui_guidelines/icon_design.html\"\n}\n"})

What I need to do to correct this?

PS: I've removed my API Key from the code to make it public, on my test the API Key is there and it is correct, since I can see the valid JSON if I click on the fetch:1 on the Safari Debugger

marnir
  • 1,187
  • 10
  • 13
Nathan Campos
  • 28,769
  • 59
  • 194
  • 300
  • 1
    The response shows that you're actually using [JSONP](http://en.wikipedia.org/wiki/JSONP), which you shouldn't parse, but let the inserted script call the callback function, that parses the parameter. This is necessary because you can't issue an XMLHttpRequest cross-domainly. – Marcel Korpel Jun 05 '11 at 14:57
  • @Marcel: But how can I do it? – Nathan Campos Jun 05 '11 at 15:20

1 Answers1

1

You forgot to set the method which the proxy (in this case: jsonlib.com) should use to POST, as is required by the goo.gl API.

Looking at the documentation of JSONlib, you should provide another property in the parameter object, method, like:

jsonlib.fetch({
    url: 'https://www.googleapis.com/urlshortener/v1/url',
    header: 'Content-Type: application/json',
    method: 'POST',
    data: JSON.stringify({longUrl: url})
}, function (m) {
    /* … */
});

Now m.content contains

'{ "kind": "urlshortener#url", "id": "http://goo.gl/ysEOJ", "longUrl": "http://tobyho.com/Trampolines_in_Javascript_and_the_Quest_for_Fewer_Nested_Callbacks" }'

which is a completely valid JSON string.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80