vjk2005 vjk2005 - 3 years ago 319
Ajax Question

Why does a "200 OK" jQuery GET request return an empty string when reading Tumblr "Liked" posts using Basic Auth?

...when I can access the data using the direct URL ยป http://www.tumblr.com/api/likes?email=myemail@gmail.com&password=mypassword&debug=1, which gives me the posts I'm looking for:

[posts] => Array
[0] => Array
[id] => 1482002179
[url] => http://staff.tumblr.com/post/1482002179
[url-with-slug] => http://staff.tumblr.com/post/1482002179/the-new-dashboard-for-iphone
[type] => photo
[date-gmt] => 2010-11-04 21:19:33 GMT
[date] => Thu, 04 Nov 2010 17:19:33
[bookmarklet] => 0
[mobile] => 0
[feed-item] =>
[from-feed-id] => 0
[unix-timestamp] => 1288905573
[format] => html
[reblog-key] => 8LVzAqTT
[slug] => the-new-dashboard-for-iphone
[liked] => 1
[note-count] => 2378
[tumblelog] => Array
[title] => Tumblr Staff
[name] => staff
[cname] =>
[url] => http://staff.tumblr.com/
[timezone] => US/Eastern
[avatar_url_16] => http://28.media.tumblr.com/avatar_013241641371_16.png
[avatar_url_24] => http://26.media.tumblr.com/avatar_013241641371_24.png

... and so on.

But no such luck when I try to achieve the same using jQuery code where I'm using this GET request with authenticated read as noted in the Tumblr REST API docs:

{ email: "myemail@gmail.com", password: "mypassword" }

I'm monitoring responses using Firebug. Status: 200 OK. Response: empty. XML: no element found.

Tracing out the response using:

{ email: "myemail@gmail.com", password: "mypassword" },
function( data ){
alert( data );

displays an empty alert message.

I get the same result no matter where I run the code from be it file:///, localhost( XAMPP ), my own server or right from within my theme on mysite.tumblr.com.

Am I missing something? I could achieve this with JavaScript + PHP, cURL etc but I'm looking for JavaScript-only solutions.

Answer Source

It sounds to me like you're running into the Same Origin Policy. You can't use Ajax to load data from just anywhere, you're limited to loading data from the same origin as the original document (not the script, the HTML document).

Assuming that's the problem, then:

If both the browser and remote site support CORS and your origin is acceptable to the remote site, you can use that. But note that right now, only some browsers support it (and IE only supports it if you make a special effort, whereas other browsers support it as part of their XmlHTTPRequest object support, which is what jQuery's get uses under the covers).

You can probably use JSONP if tumblr supports it, though, which jQuery has support for in its .get call (not .GET as in your code examples).

Another option is to use Yahoo! Query Language as a cross-domain proxy.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download