views:

171

answers:

4

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?

A: 

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
Ajax is very much allowed to cross domains.
Joel Potter
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
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 Potter
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
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 Potter
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
Sorry for vote blocking you. I do the same thing with my answers sometimes. ;-)
Joel Potter
A: 

Try appending callback=? to the URL. Like this

"http://twitter.com/statuses/user_timeline/username.json?count=1&callback=?"
jitter
A: 

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

XGreen
+6  A: 

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