3

I have a very simple bit of jQuery to retrieve my latest Tweet

$.getJSON("http://twitter.com/statuses/user_timeline/username.json?count=1", 
           function(data) {
              $("#tweet_text").html(data[0].text);
           });

This works fine on a simple HTML file on my desktop. However, once the file is accessed from my localhost (apache) no data is being returned. I was wondering if any part of Apache was blocking the request somehow? Or any other ideas?

nicerobot
  • 9,145
  • 6
  • 42
  • 44
fatnic
  • 499
  • 6
  • 12
  • 1
    Your browser executes the JS, not Apache, so Apache is not to blame. You really need to be a lot more specific than "it won't work". What headers are returned from Twitter? Status code? – Amy B Mar 23 '10 at 23:50

4 Answers4

7

JavaScript cannot currently make direct requests cross-domain due to the Same-origin Policy.

You're best bet is probably to look into JSONP for this.

You can find more information on it from both jQuery:

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

and Twitter:

Parameters:

  • callback: Optional. Only available for JSON format. If supplied, the response will use the JSONP format with a callback of the given name.

    • Example: http://search.twitter.com/search.json?callback=foo&q=twitter

Hope this helps.


Correction...

If status/user_timeline supports JSONP, it's not documented as such.

You may have to look into setting up a Cross-Domain Proxy to get the data you want.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
0

This is a shorthand Ajax function, which is equivalent to:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: success
});

Ajax (Non-JSONP) is not allowed to cross domains.

nicerobot
  • 9,145
  • 6
  • 42
  • 44
  • Ajax is very much allowed to cross domains. – Joel Mar 23 '10 at 23:58
  • You have it backwards! XMLHttpRequest is only allowed cross-domain if access-controls are in use and not all browsers support it and few servers probably enable it. You can most certainly load and execute scripts served by other domains. For example, it's how google analytics works. And i've written hundreds of scripts that load jQuery directly from http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js as well as use http://code.google.com/apis/ajax/documentation/#GoogleLoad – nicerobot Mar 24 '10 at 00:02
  • Practically every browser will allow you to get data from any external domain using JS. XSS refers to scripts which attempt to interact with the scripts or the DOM on another domain (window-to-window, frame-to-frame, etc.). Simply calling data from another domain should not invoke XSS filters. Working example: http://jsbin.com/okoko/3/edit (in which jsbin.com loads a json resource from stackoverflow.com) – Joel Mar 24 '10 at 00:19
  • Your script is NOT ajax, i.e. XMLHttpRequest! It works by creating a script tag which loads the JSONP as javascript and it is executed the moment it's loaded into the page. But without JSONP, the .getJSON will fail because cross-domain access is not allowed with standard ajax! – nicerobot Mar 24 '10 at 00:35
  • True, it's not an XMLHttpRequest. I guess it's just Aj. But to many amateurs the difference is not intuitive. I'll take off my down vote because your answer is technically correct, though I maintain it is not useful. Useful would be to mention the same origion policy, and how jsonp differs from ajax. As it stands, this would not help solve the problem. – Joel Mar 24 '10 at 00:51
  • Yea, i was going to get to that until i had to start defending the post =P and then @jonathan-lonowski's post appeared and it became a moot point because no one was going to look at mine. I guess i learned a lesson. Type faster, be more complete with the first response and don't edit tags to broaden then audience until the post is complete. – nicerobot Mar 24 '10 at 01:00
  • Sorry for vote blocking you. I do the same thing with my answers sometimes. ;-) – Joel Mar 24 '10 at 01:14
0

Try appending callback=? to the URL. Like this

"http://twitter.com/statuses/user_timeline/username.json?count=1&callback=?"
jitter
  • 53,475
  • 11
  • 111
  • 124
0

maybe it takes a bit longer for the html to load on localhost for some reason and you haven't wrapped the script in a dom ready. so it makes a call and there is no #tweet_text at that moment to get filled

makeitmorehuman
  • 11,287
  • 3
  • 52
  • 76